LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-06-2013, 07:16 AM   #1
gregmcc
Member
 
Registered: Mar 2007
Posts: 41

Rep: Reputation: 1
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?

Last edited by gregmcc; 08-06-2013 at 07:34 AM.
 
Old 08-06-2013, 07:27 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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
 
1 members found this post helpful.
Old 08-06-2013, 07:33 AM   #3
gregmcc
Member
 
Registered: Mar 2007
Posts: 41

Original Poster
Rep: Reputation: 1
Thanks a lot! Perfect!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
[SOLVED] 'for' loop in bash script vjramana Programming 6 05-13-2012 11:04 AM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 11:27 PM
bash script loop bong.mau Programming 6 09-14-2005 08:38 PM
bash script for loop drisay Programming 5 12-25-2004 01:32 AM


All times are GMT -5. The time now is 02:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration