Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ignore header lines 1

Status
Not open for further replies.

sjnb

Technical User
Jun 13, 2001
4
GB
Hi All,

Can any body tell me how to ignore the first and last 3 lines in a source file that needs processing

Thanks
SJNB
 
Is there any other way of distinguishing these records?

Greg.
 
Hi, snjb!

To ignore the first 3 lines in a source file that needs processing put this statement on first place in awk program:

NR < 4 { next }

For example, this awk script print lines from 4th line to end line:

NR < 4 { next }
{ print }

Bye!

KP.

 
The problem is with the footer as in awk you don't know
the number of records in a file before the &quot;END&quot; block.
Therefor, you don't know whether you're processing a footer
or you're still in the &quot;body&quot; of the file.

The easiest way is to calculate the number of records/lines
in a file prior to calling awk/nawk and pass the &quot;begin/end&quot;
pivots to awk.

two step approach:

---------- filterHeaderFooter.ksh --------------------------
#!/bin/ksh

let header=1;
let footer=1;
numLines=`cat $* | wc -l | sed -e 's/[ ]*//g'`

let begin=${header}
let end=numLines-footer

echo &quot;begin-> ${begin} end-> ${end}&quot;

nawk -v begin=$begin} -v end=${end} -f filterHeaderFooter.awk $*

-------------------------------------------------------------

---------------filterHeaderFooter.awk ----------------------
BEGIN {

}

FNR > begin && FNR < end { print $0 }

END {

}
 
Hi sjnb,

This should do it for you:


#!/bin/sh

count=`wc -l $1`
last=`expr $count - 3`
nawk 'NR == 4, NR == '&quot;$last&quot;' {print}' $1 > $1.new

Hope this helps you!


flogrr
flogr@yahoo.com

 
If you want to do it all in awk, try this.

# awk program to strip first and last 3 lines from a file.
{
if (NR == 4)
hld[0] = $0
else if (NR == 5)
hld[1] = $0
else if (NR == 6)
{
hld[2] = $0
ix = 0
}
else if (NR > 6)
{
print hld[ix]
hld[ix] = $0
ix++
if (ix > 2) ix=0
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top