Operating System - HP-UX
1819926 Members
2914 Online
109607 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...
Rodney Hills
Honored Contributor

Re: grep using awk then head and tail

A slight change to my previous "sed"

sed -n -e "`grep -n \"mno\" infile | awk -F: '{print $1 - 1 \",\" $1 \"p\"}'`" infile

I needed to put the result of the grep in double quotes following the -e option of sed.

The \" are there so the shell will not treat them as matching the first ".

This will now work for multiple finds of $name1.

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

Re: grep using awk then head and tail

Well, I never got exactly what I wanted from my buddy grep. A young lady next to me wrote (in a minute or so) a perl script that did the trick. It parses the file in short order....maybe I should have used "C".....
#!/usr/perl5/bin/perl5.00502
$srcfil = "cmaudit" ;
$srcdir = "/tmp/autosys/" ;
$outdir = "/tmp/autosys/" ;
$outfil = "sstimes.log" ;
##################################
# Open files
#
open ( INPFL, "${outdir}${srcfil}" ) ||
die "Couldn't open ${outdir}${srcfil}" ;
open ( OUTFL, ">${outdir}${outfil}" ) ||
die "Couldn't open ${outdir}${outfil}" ;
while ( )
{
# chop ;
$line = "$_" ;
$begin = index($line, "BEGIN" ) ;
if ( $begin != -1 ) {
printf (OUTFL "%s", $line) ;
$nline = ;
printf (OUTFL "%s", $nline) ;
} else {
$finish = index($line, "FINISH" ) ;
if ( $finish != -1 ) {
printf (OUTFL "%s", $oline) ;
printf (OUTFL "%s", $line) ;
}
}
$oline = "$_" ;
} #End of while input
close (INPFL);
close (OUTFL);
exit;

Thanks for all the GREAT ides though!
Some days you get the bear, blah, blah
~jdk
Dare to Dream
harry d brown jr
Honored Contributor

Re: grep using awk then head and tail

cat -n cmaudit | grep BEGIN | 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
cat -n cmaudit | grep BEGIN | awk '{print $1}' | head -n '{print $1}' cmaudit | tail -5 . Can you point out the error of my ways?
Live Free or Die
harry d brown jr
Honored Contributor

Re: grep using awk then head and tail

sorry about that last post, PLEASE IGNORE.


HOW ABOUT THIS:


cat -n cmaudit |
grep -e BEGIN -e FINISH |
awk 'BEGIN {pos_beg=0;pos_end=0;}
{if (pos_beg == 0) {pos_beg=$1;next;}
if (pos_end == 0) {pos_end=$1;next;}
} END {printf "head -n %d %s \| tail -n %s\n",pos_end,"cmaudit",pos_end-pos_beg+
1;}' |
xargs -i sh {}


live free or die
harry

Live Free or Die
TrustNo1
Regular Advisor

Re: grep using awk then head and tail

Harry, the last one extracts ALL of the first record BEGIN to FINISH. If you reeeeally want to, there is a sample datafile attached!

~jdk
Dare to Dream
harry d brown jr
Honored Contributor

Re: grep using awk then head and tail

So you want something like this:

cat cmaudit |
awk 'BEGIN {startdisp=0;}
{if (match($0,/.*BEGIN.*/) > 0) {startdisp=1; }
if (startdisp == 1) {print $0;}
if (match($0,/.*FINISH.*/) > 0) {startdisp=0;}
}'


Will print every BEGIN-to-FINISH section.

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

Re: grep using awk then head and tail

Harry,
That also puts out too much.
Only need;
The BEGIN line and the line after it.
The FINISH line and the line before it.

~jdk
Dare to Dream
harry d brown jr
Honored Contributor

Re: grep using awk then head and tail

Like this:

cat cmaudit |
awk 'BEGIN {startdisp=0;}
{
if (match($0,/.*BEGIN.*/) > 0) {print $0; startdisp=1; next;}
if (startdisp == 1) {print $0;startdisp=0;next;}
if (match($0,/.*FINISH.*/) > 0) {print prevline; print $0;startdisp=0;}
prevline=$0;
}'


******BEGIN CIMCCV001******
Thu May 30 18:02:17 EDT 2002
Thu May 30 18:02:19 EDT 2002
******FINISH CIMCCV001******
******BEGIN CIMCCV002******
Thu May 30 23:52:31 EDT 2002
Thu May 30 23:52:31 EDT 2002
******FINISH CIMCCV002******
******BEGIN CSICSY001******
Thu May 30 18:56:16 EDT 2002
Thu May 30 18:56:54 EDT 2002
******FINISH CSICSY001******
******BEGIN CSICSY004******
Thu May 30 18:56:44 EDT 2002

******FINISH CSICSY004******


though the last finish didn't have a time, except after it!

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

Re: grep using awk then head and tail

BINGO!

This runs as fast as the perl.
Unfortunatly, I'm not gifted enough to have figured out this on my own :( .
Well, thank you, I am going to keep this one for reference for a long time I hope!

Have a GREAT weekend Harry,
~jdk
Dare to Dream
Rodney Hills
Honored Contributor

Re: grep using awk then head and tail

If you wanted lines from BEGIN to FINISH, then the simple-

sed -n -e '/BEGIN/,/FINISH/p' $file

Is all you need...

-- Rod Hills
There be dragons...