GJO
GJO

Reputation: 11

how to search trough a file for a particular word

For example I have a file with words

jogging:1 swimming:1 running:1 breathing:1
riding:1  walking:1  jogging:2  seeing:1
seeking:1 looking:1 staring:1 practicing:1

And I want to search the whole file for that word if possible with a loop(for or while) and that word can appear multiple times So it would return this if I needed to search for the word jogging

jogging:1
jogging:2

Upvotes: 0

Views: 46

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185851

Like this:

$ grep -oP 'jogging:\d+' file
jogging:1
jogging:2

or with an old grep:

$ grep -o 'jogging:[0-9]\+' file
jogging:1
jogging:2

If you insist to use a for loop:

set -f

for word in $(< file); do
    [[ $word =~ jogging:[0-9]+ ]] && echo "$word"
done 

I take advantage of word splitting here

Upvotes: 1

Related Questions