Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I have tons of books, have book marked tons of tutorials, which have helped, but this forum has answered those "impossible to find" solutions. I am thrilled with this site..."

Geography

Where in the world do Tek-Tips members come from?

Ignore header linesHelpful Member! 

sjnb (TechnicalUser)
21 Jun 01 8:56
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
grega (Programmer)
21 Jun 01 9:43
Is there any other way of distinguishing these records?

Greg.
dbmsguy (MIS)
21 Jun 01 9:47
if you are taking a file and doing a bulk insert into a table you can specify the starting row to use (same with bcp).

Paul
www.dbmsguy.com

dbmsguy (MIS)
21 Jun 01 9:49
oops, sorry thought i was in a different forum

Paul
www.dbmsguy.com

dbmsguy (MIS)
21 Jun 01 9:55
cat filename | sed 1d |sed 1d |sed 1d

this will delete the first three lines

Paul
www.dbmsguy.com

Helpful Member!  Krunek (Programmer)
21 Jun 01 11:44
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.

vgersh99 (Programmer)
21 Jun 01 11:56
The problem is with the footer as in awk you don't know
the number of records in a file before the "END" block.
Therefor, you don't know whether you're processing a footer
or you're still in the "body" 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 "begin/end"
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 "begin-> ${begin} end-> ${end}"

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

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

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

}

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

END {

}
flogrr (Programmer)
21 Jun 01 15:15
Hi sjnb,

This should do it for you:


#!/bin/sh

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

Hope this helps you!


flogrr
flogr@yahoo.com
 
 
CaKiwi (Programmer)
22 Jun 01 10:44
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
  }
}

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close