LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2011, 11:58 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
how to grep(1) loop output


I have a following endless loop checking for lost packets:

Code:
while [ 1 = 1 ]; do ping -f -i1 -c1 192.168.1.1 | while read i; do echo "$(date "+%T %d.%m"): $i" ;done ;done | grep "\: \."
..which generates following output:

Code:
:~> while [ 1 = 1 ]; do ping -i1 -f -c1 192.168.1.1 | while read i; do echo "$(date "+%T %d.%m"): $i"; done; done | grep '\: \.'
18:50:00 24.03:  
18:50:00 24.03:  
18:50:00 24.03:  
18:50:00 24.03:  
18:50:00 24.03:  
18:50:00 24.03:  
18:50:00 24.03:  
18:50:00 24.03:  
18:50:00 24.03:  
18:50:10 24.03: .
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:10 24.03:  
18:50:11 24.03:  
18:50:11 24.03:  
18:50:11 24.03:  
18:50:11 24.03:  
18:50:11 24.03:  
18:50:11 24.03:  
18:50:11 24.03:  
18:50:21 24.03: .
18:50:31 24.03: .
18:50:31 24.03:  
18:50:41 24.03: .
18:50:41 24.03:
However, there are few problems. How to stop this? I mean "Ctrl + c" does not work And the main thing is, why does it print lines which do not contain ": ." pattern?
 
Old 03-24-2011, 12:04 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by m4rtin View Post
And the main thing is, why does it print lines which do not contain ": ." pattern?
echo "$(date "+%T %d.%m"): $i" is printing the date lines.

I guess you could do something like:

Code:
#!/bin/bash
while true;
do
 i=`ping -f -i1 -c1 192.168.1.1`
 d=$(date "+%T %d.%m")
 if [[ $i == *dropped* ]]
  then
  echo "$d: Packet Lost";
 fi
done;
I used the word 'dropped' because I'm not sure what exactly you were grepping for. But this will sit there with no output, until it sees the word "dropped" then it will display the time and Packet Lost like:
Code:
13:30:08 24.03: Packet Lost

Last edited by szboardstretcher; 03-24-2011 at 12:34 PM.
 
  


Reply



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
Grep in a for loop. rose_bud4201 Programming 13 03-18-2009 04:02 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-30-2009 11:21 PM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 10:27 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:29 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
Open Source Consulting | Domain Registration