Operating System - HP-UX
1752623 Members
4336 Online
108788 Solutions
New Discussion юеВ

grep using awk then head and tail

 
SOLVED
Go to solution
TrustNo1
Regular Advisor

grep using awk then head and tail

Im am try to retreive the line number of a grep return using awk and pass it to head -n then tail -2.
My feeble attempts need help!
I'mwas attempting to use;
cat -n $file | grep PARM | awk '{print $1}' | head -n ${1} $file .
the cat -n $file | grep PARM | awk '{print $1}' works fine, but how do I pass that to head -n ?
Thank you ~jdk
Dare to Dream
19 REPLIES 19
Rodney Hills
Honored Contributor
Solution

Re: grep using awk then head and tail

To pass as a parameter, you would use the execute in place shell ability.

example-

head -n $(echo 4) filename


In your example, replace all those commands for the "echo 4".

Their might be an easier way to do what you are trying to do. Can you rephrase your question in terms of what results you are looking for.

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: grep using awk then head and tail

cat -n $file | grep PARM | awk '{print $1}' | xargs -i head -n {} $file


live free or die
harry
Live Free or Die
steven Burgess_2
Honored Contributor

Re: grep using awk then head and tail

Hi

Use

cat $file | grep PARM | awk '{print $1}' | head -10

HTH

Steve

take your time and think things through
harry d brown jr
Honored Contributor

Re: grep using awk then head and tail


Sample using an IOSCAN I did to a file:


cat -n $file | grep C895 | awk '{print $1}' | xargs -i head -n {} $file
Class I H/W Path Driver S/W State H/W Type Description
============================================================================
root 0 root CLAIMED BUS_NEXUS
ioa 0 0 sba CLAIMED BUS_NEXUS System Bus Adapter (803)
ba 0 0/0 lba CLAIMED BUS_NEXUS Local PCI Bus Adapter (782)
lan 0 0/0/0/0 btlan CLAIMED INTERFACE HP PCI 10/100Base-TX Core
/dev/diag/lan0 /dev/ether0 /dev/lan0
ext_bus 0 0/0/1/0 c720 CLAIMED INTERFACE SCSI C895 Fast Wide Single-Ended
#


live free or die
harry
Live Free or Die
TrustNo1
Regular Advisor

Re: grep using awk then head and tail

Dang you guys are good, but I need to rephrase.
I need to go through a file, find all occurences of $name1, then echo out that line and the line prior to it.
I also need to find $name2 and echo out that line and several after it.
ie If I already know the line #, I can use "head -n line# | tail -1 or head +4.
Does that make more sense?

Thanks again, ~jdk
ps ALL of you get and deserve 10 points eitherway
Dare to Dream
harry d brown jr
Honored Contributor

Re: grep using awk then head and tail

Try playing with this, it'll blow your mind:


cat -n $file | grep C895 | awk '{printf "head -n %d %s \| tail -n %s\n",$1+2,"XZ
X",$1-1;}' | sed "s,XZX,${file},g" | xargs -i sh {}





live free or die
harry
Live Free or Die
Rodney Hills
Honored Contributor

Re: grep using awk then head and tail

Here is a way with perl-

perl -n -e '$s2=$s1;$s1=$_;/mno/ && do {print "$s2$s1"}' $file

Hope this helps...

-- Rod Hills
There be dragons...
TrustNo1
Regular Advisor

Re: grep using awk then head and tail

I originally thought this would work;
cat -n cmaudit | grep BEGIN | awk '{print $1}' | head -n '{print $1}' cmaudit | tail -5 . Can you point out the error of my ways?

Harry, I'm using a test machine so I tried your mind blower, modified of course....it returned empty. I'm not quite sure I understand all it does, or if I could understand it anyway!

I will give the perl scripting a try, and get back to you Rod.

Thanks again! ~jdk
Dare to Dream
Rodney Hills
Honored Contributor

Re: grep using awk then head and tail

Here is a more generic version-

sed -n -e `grep -n $name1 $file | awk -F: '{print $1 - 1 "," $1 "p"}'` $file
sed -n -e `grep -n $name2 $file | awk -F: '{print $1 "," $1 + 7 "p"}'` $file

The first finds $name1 and prints line prior and current line.

The second finds $names and prints line plus 7 additional lines.

Hope this helps.

-- Rod Hills
There be dragons...