LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script - grep in a loop (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-grep-in-a-loop-4175472311/)

gregmcc 08-06-2013 06:16 AM

Bash script - grep in a loop
 
For some reason i am struggling with the following.

I have a command that greps out data from a text file:
Code:

cat file | grep Registration | awk '{print $3}'
This prints out:
10
11
22
32

I want to check if any of the values = 100 and then echo "Error"

I know I need to use a for loop but then how do I specify the number of times to loop, and if I put the grep in the loop wont it only spit out the first match each time?

druuna 08-06-2013 06:27 AM

Is this what you are looking for:
Code:

awk '/Registration/ { if ( $3 == "100" ) { print $3, "error" } else { print $3 } }' infile
Example run:
Code:

$ cat infile
Registration blaat 10
Registration foobar 11
Registration foo 100
Registration bar 22
Registration bar 32

$ awk '/Registration/ { if ( $3 == "100" ) { print $3, "error" } else { print $3 } }' infile
10
11
100 error
22
32


gregmcc 08-06-2013 06:33 AM

Thanks a lot! Perfect!


All times are GMT -5. The time now is 12:56 PM.