How to grep and get only matching string?

4

Feel free to change the title as it does not match my question 100%

I have something like this in a file:

junk
long_ass_string "/I/want/this/$code/$name" long_ass_string
junk

Clarifying the example:

  • The /I/want/this/ part is always the same
  • $code and $name are dynamic and different for each string
  • Inside long_ass_string there can be more /I/want/this/$code/$name strings and I would like to get all of them.
  • The quotation marks (this => ") are present in every /I/want/this/$code/$name string.

So far I've tried...

grep -w "/I/want/this/*" file # outputs long_ass_string
grep -o "/I/want/this/*" file # outputs /I/want/this/

Would like to avoid using the solution of getting only x extra characters before and/or after

sysfiend

Posted 2017-02-28T13:23:08.233

Reputation: 417

2Your second command is nearly right, but there are two issues: the quotes are parsed out by bash and grep doesn't see them; and the wild-card * is different between grep and bash: the * in bash is equivalent to .* in grep. so what you need is grep -o '"/I/want/this/.*"' – AFH – 2017-02-28T14:45:49.647

@AFH that's true but executing it returns long_ass_string – sysfiend – 2017-02-28T15:13:03.160

It didn't for me: echo 'long_ass_string "/I/want/this/$code/$name" long_ass_string'|grep -o '"/I/want/this/.*"' returns "/I/want/this/$code/$name". – AFH – 2017-02-28T16:22:42.690

Answers

4

I would go with grepping all strings and then sort it out with a second grep, e.g.:

grep -o '"[^"]*"' file

Output:

"/I/want/this/$code/$name"

Comment on your use of regular expressions

This expression /I/want/this/* matches /I/want/this and then zero or more slash characters, you probably meant: /I/want/this/.* which matches /I/want/this/ and zero or more characters.

Thor

Posted 2017-02-28T13:23:08.233

Reputation: 5 178

0

if i understand you well you want to get ridd off the first $code and $name variable in each line. You can pipe your result to cut for that. Following your example:

grep "/I/want/this/" myfile.txt | cut -d '/' -f 1-4,7-

with -d you define the delimiter ( the symbol / for instance) and with -f you indicate the field to grab.
In this case from delimiter 1 to 4 (/I/want/this/ ) and all the fields that come after the seventh delimiter (that is done with argument 7-) Like this you takes away /$code/$name which are between 4th and 7th delimiter in all lines that match the defined regular expression.

echo "/I/want/this/NOT/THAT/and/everythingelse" | grep "/I/want/this/" | cut -d '/' -f 1-4,7- 
/I/want/this/and/everythingelse

kcdtv

Posted 2017-02-28T13:23:08.233

Reputation: 101

1you can just grep "/I/want/this/" myfile.txt . Cat abuse isn't necessary. – Journeyman Geek – 2017-02-28T14:41:11.660

No, I can not cut the result as there can be more than one matching string in the long_ass_string so using cut would give me just the first one – sysfiend – 2017-02-28T15:11:10.253

I think i don't undesrtand what you realy want to do... Otherwise the command take everything after $name even if the regular expression is present several timesecho "/I/want/this/NOT/THAT/and/everythingelse/even/if/I/want/this/is/here/again" | grep "/I/want/this/" | cut -d '/' -f 1-4,7- The result is /I/want/this/and/everythingelse/even/if/I/want/this/is/here/again No matter how many occurrences you have, the entire line is grabbed with one or two matches. – kcdtv – 2017-02-28T18:59:16.410