0

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 

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Word splitting yes, but also filename expansion. You might consider set -f

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.