Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

A script must be written to extract and return a string value from a file. This must be done only after a match is found in the file for the given pattern.

Example:

"id":"re454sv:57ghg34sfw:87wre56:rty4598gh" 

This text is the sub part of a big log file and also many instances of this with "id":"some value" could occur. I need to return that 'some value' portion every time and from a lot of files in that directory.

I think SED utility in Unix helps here, but I couldn't figure out ts exact usage. Would be glad f somebody could help. Thanks for you time.

share|improve this question

migrated from serverfault.com May 1 at 15:51

This question came from our site for system and network administrators.

2 Answers 2

sed 's/"id":"\([^"]*\)"/\1/g'

Extracts the value if you pipe it in via stdin

share|improve this answer
2  
Please explain your answer. Bare sed scripts are hard to understand, especially for a questioner who's not familiar with sed. –  Andrew Schulman May 1 at 11:58

You're not clear on whether you want just the first : separated value or the whole remainder of the line after the "id" tag.

The former is:

sed -nre 's/^"id":"([^:]*):.*/\1/p' <file>

The latter is:

sed -nre 's/^"id":(.*)/\1/p' <file>

Note the switches are important. -n ensures that nothing is printed (with the p tail printing those lines that do match). -r enables extended regular expression parsing so things like brackets don't have to be escaped.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.