How to extract a whole word containing substring

command linetext processing

If I have a file that looks like this:

example.png example.jpg example2.jpg example2.png example.swf
example2.swf example3.jpg example4.jpg example3.png example3.swf 

and I want to extract the words containing swf for example, the output would look something like this

example.swf 
example2.swf
example3.swf

I tried grep -o "swf[^[:space:]]*", which just prints a bunch of swf, and then I tried grep -o '[^ ]*a\.swf[^ ]*', which output very few lines containing "swf". Does anyone know what to do?

Best Answer

With GNU grep:

grep -o '\b\w*\.swf\b' file

Output:

example.swf
example2.swf
example3.swf

\b: a zero-width word boundary

\w: word character

\.: match one dot


See: The Stack Overflow Regular Expressions FAQ

Related Question