简体   繁体   中英

How to find this pattern with grep in a shell script?

How can i find this pattern in grep?

1.1.1.1 (everything) testString (everything)

I means i need a search that can find out if testString is there in each of this case:

1.1.1.1 test1 testString test2
1.1.1.1 test1 test2 testString
1.1.1.1 testString test1 test2

Then, how can i get the result of the grep in form of "false" (not found) or "true" (found it) in order to pass the result to an if clause inside a shell sh script?

Your question isn't very clear. Are you looking for testString or 1.1.1.1 followed by testString ?

Assuming the second case, and assuming you're reading in from a file:

while read line; do
  echo $line | grep -q '1\.1\.1\.1.*testString' && echo found || echo not found
done < filename.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM