stry-kai
stry-kai

Reputation: 493

How to test if a string exists in a file and if so, check the remaining lines in the file for another string with bash shell

I already have a log say file.txt.

I will like to check in Bash, if a string (July) exists in the log and if so, check the remaining lines in the log for another string (foobar).

Upvotes: 2

Views: 302

Answers (1)

Kent
Kent

Reputation: 195289

if you have awk available, an awk oneliner could do it for you:

awk '/July/{f=1} f&&/foobar/{print "Found foobar after July!";exit}' file

if no July or no foobar after July, the command prints nothing. of course you could make an if-else, to let it print "not found" or something.

Upvotes: 5

Related Questions