0

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.

1
  • How do you tell the first part from the second part? Commented Mar 8, 2012 at 2:55

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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:

Comments

2
a="dimensioning-inspection_depth-0rules_20120306-084158"
echo $a | head -c -16

So as the timestamp will always be equal width (16 characters that you are trying to trim off the end in this case), head -c -16 should work for you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.