Skip to content
Advertisement

How to GREP words, not lines, that contain specific characters, and print entire word

I have a file with tons of lines and words such as this example:

JavaScript

What I want to do is list only the word (assuming each 4 character bundle is a word) that contains a specific number, such as 35.

In this example, I would want the result printed to be:

JavaScript

I have tried a few different ways such as using grep only to find either the entire line that contains a 35 gets printed, or grep -o 35 only the 35 gets printed and I do not know what the prefix of that number was.

Advertisement

Answer

Try the following bash script:

JavaScript

Explanation:

cat reads words.txt and it spits them out to STDOUT, that gets piped into tr which means “translate”: In this case from space (” “) to newline (“n”), then, grep just does its default line-by-line behaviour and searches for anything containing 35.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement