the problem I met is like this:
given a string dimensioning-inspection_depth-0rules_20120306-084158
I'd like to get the first part of the string: dimensioning-inspection_depth-0rules, so to speak, get rid of the time stamp part. In perl, it's like a piece of work to do the job using regular expression. But since I'm new to bash, I'd like to know
the best-practice way of doing it in Bash.
-
How do you tell the first part from the second part?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2012-03-08 02:55:14 +00:00Commented Mar 8, 2012 at 2:55
Add a comment
|
3 Answers
If you don't want to pipe the string to sed, you can use Bash variable mangling:
$ string=dimensioning-inspection_depth-0rules_20120306-084158
$ echo ${string%_*}
dimensioning-inspection_depth-0rules
More information on variable mangling here.
Comments
You can do this:
str=dimensioning-inspection_depth-0rules_20120306-084158
echo ${str%_*}
This will remove anything after and including the last _ in the string, so if your string always has the form something_date-time, you will always be left with something part.
More info here: