tail, grep and cut | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

tail, grep and cut

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-03-2008
sylaan sylaan is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 31 July 2008, 8:09 AM EDT
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
tail, grep and cut

Hello all,

I have some weird problem that kinda baffles me. Say I have the following test file:


Code:
claudia:~/tmp$ cat testfile.txt
This is a test line
This is the second test line
And yeah, this is the third test line

Then say I want to tail the file, grep for the word "third" then display only the 7th field. I would do some like this:


Code:
claudia:~/tmp$ tail -f testfile.txt | grep third | cut -d\  -f7

However, that does not work. I get no error, but no output either. If I leave out the "cut" part, then the grep works fine:


Code:
claudia:~/tmp$ tail -f testfile.txt | grep third
And yeah, this is the third test line

If I do the same with "cat" then it works fine, the expected output appears:

Code:
claudia:~/tmp$ cat testfile.txt | grep third | cut -d\  -f7
test

What am I doing wrong ?

Thanks,
Sylaan
Sponsored Links
    #2  
Old 03-03-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 28 July 2014, 1:31 PM EDT
Location: Out running a Marathon.
Posts: 2,321
Thanks: 68
Thanked 153 Times in 142 Posts
Question Confused about some of your syntax

What are you trying to accomplish with the tail -f command?
And then your grep "third"?

Are you simply trying to narrow your search to the last ten lines (default of the tail command), and then find the line with the word "third"? If so, try without the -f on the tail command.

By the way, I am not convinced that your example works fine when you think it does. The -f on the tail command would appear to keep the process running awaiting additional input.
Sponsored Links
    #3  
Old 03-03-2008
sylaan sylaan is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 31 July 2008, 8:09 AM EDT
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
It was just an example to illustrate a real-world problem that I have.

Basically I need to tail a syslog file, grep for a certain pattern then display a certain field from the matched line. What I wanted to say with my example is that the "cut" after the "grep" is not producing anything, even though it should (as far as I can tell). It does not matter if I tail a syslog file or a testfile created by me, it should still work.

And sure, tail -f waits for more input but that does not matter, by default it should still list the last 10 lines or so from the file, which is plenty of input for my grep and cut.

--
Sylaan
    #4  
Old 03-03-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
i will let others explain to you. in the meantime, you can try this out

Code:
# tail -f file | awk '/third/{print $7}'

Sponsored Links
    #5  
Old 03-03-2008
sylaan sylaan is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 31 July 2008, 8:09 AM EDT
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ghostdog74 View Post
i will let others explain to you. in the meantime, you can try this out

Code:
# tail -f file | awk '/third/{print $7}'

Nice, that works fine :-) Thanks. One more quick question: how would I print everything from the 7th field to the end of the line , in awk ? Something like cut's -f 7- .
Sponsored Links
    #6  
Old 03-03-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 28 July 2014, 1:31 PM EDT
Location: Out running a Marathon.
Posts: 2,321
Thanks: 68
Thanked 153 Times in 142 Posts
Question only one command piped after a tail -f ??

Trying this out on several files, because the -f keeps the process open, appears to therefore not allow any additional commands after the first pipe.
tail -f file | grep val1 >will work
tail -f file | grep val1 | grep val2 >will not work

but,
tail file | grep val1 | grep val2 > will work
Sponsored Links
    #7  
Old 03-03-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
Quote:
Originally Posted by sylaan View Post
Nice, that works fine :-) Thanks. One more quick question: how would I print everything from the 7th field to the end of the line , in awk ? Something like cut's -f 7- .
normally you use a for loop

Code:
tail -f file | awk '/third/{ for(i=7;i<=NF;i++) print $i }'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
mulitple grep using tail robertson1995 UNIX for Dummies Questions & Answers 3 09-02-2010 05:03 PM
Need help with a tail and a grep Silver11 UNIX for Dummies Questions & Answers 3 03-10-2010 10:01 AM
tail | grep sdilucca Shell Programming and Scripting 6 01-22-2009 03:41 PM
tail | grep lagging badly WasabiVengeance Shell Programming and Scripting 3 04-22-2008 06:53 PM
Help with tail /grep needed Felix2511 UNIX for Dummies Questions & Answers 4 05-17-2007 03:22 PM



All times are GMT -4. The time now is 06:32 PM.