1847855 Members
2682 Online
104021 Solutions
New Discussion

Re: Something like grep

 
Ed Hon
Regular Advisor

Something like grep

In a script, I have a third party command that produces three lines of output. I need to "grep" the third line but don't want to specifiy a pattern. How do I do that? TIA.
7 REPLIES 7
Stephane Vinsot
Advisor

Re: Something like grep

just

| tail -1

it will display the last line
John Palmer
Honored Contributor

Re: Something like grep

One way is to use a combination of 'head' and 'tail'. To get the third line from a file:-

head -n 3 | tail -n 1

Regards,
John
Victor BERRIDGE
Honored Contributor

Re: Something like grep

tail -n1
e.g.
vi gaga
werthvkjhmrlgvhlmrmv
weitvjmhmrev
wethmlrhg
"gaga" [New file] 3 lines, 44 characters
alphard:/home/logadm # tail -n1 gaga
wethmlrhg
alphard:/home/logadm #
Alan Riggs
Honored Contributor

Re: Something like grep

Just to be different:

| sed -n 3p
Rick Garland
Honored Contributor

Re: Something like grep

I use the sed -n 3p

Can also do sed -n 1-3p to print the lines 1-3
Stephane Vinsot
Advisor

Re: Something like grep

Just to be ridiculous ;)

| (read line ; read line ; read line ; echo $line)

yes, it prints the 3rd line...
Alan Riggs
Honored Contributor

Re: Something like grep

Just because:

| awk 'NR == 3 {print}'