0

How to extract the bold number from the string below using unix shell script?

17: H.0(-2073):File ID (40008)in xyz file not equal to the file ID(**40004**)in file header.

Thanks :)

1 Answer 1

3
echo '17: H.0(-2073):File ID (40008)in xyz file not equal to the file ID(40004)in file header.' | sed -e 's/.*(\([0-9]*\)).*/\1/'

The second part of this line runs sed with command s (substitution). Part between first two slashes (/) is regular expression which matches the following:

Everything (.*) in greedy manner, i.e. until the last occurrence of any number of digits in brackets ( ([0-9]*) ) and then everything again (.*) until the end of line. Expression between \( and \) (i.e. 40004 in this case) is memorized to be used in the second part of s command.

The part between the second / and third / is what we want to place instead of the line matched with regular expression. Here it is \1, meaning reference to the substring between 1st occurrence of \( and \) which is 40004 in our case.

So the part after | replaces the whole input string with string 40004 extracted from it. Regular expressions are powerful but often write-only technique, so I hope this explanation will bring a bit more clarity.

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

2 Comments

could you please explain me the code ""sed -e 's/.*(([0-9]*)).*/\1/' "" How is it able to cut only Bold number?
@KK Added some explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.