Another text filtering question

I want to remove everything from a file but the word following the search word.

Example:

crap crap crap crap SearchWord WordToKeep crap crap crap

How would I do this with say awk or grep?

Thank you!

{ wordtokeep=$(sed -e "s/.*SearchWord \([^ ]*\) .*/\1/g" file); echo "$wordtokeep" > file; }

Sorry to correct you, vino, but your code will work only in most cases, not in all: if the WordToKeep is the last word in the line it will fail because you require a space after it.

On the first line your code will work, on the second it will fail:

"crap crap searchword wordtokeep crap"
"crap crap searchword wordtokeep"

It will also fail in cases where the WordToKeep is followed by some punctuation mark like it is the case in texts:

"crap crap searchword wordtokeep, crap"

I therefore suggest the following enhancement to your code (<spc> and <tab> are characters, only encoded here for readability):

sed -e "s/.*SearchWord \([^<spc><tab>;,.!?":][^<spc><tab>;,.!?":]*\)/\1/g" file

bakunin

Thanks for that.

Cheers !

Well I knew it wasn't all that easy. Thanks for the answers.