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!

Limit awk test to a range of lines

Status
Not open for further replies.

gxh1000

Programmer
Aug 4, 1999
10
US
My input file has a header & trailer line which must remain intact in the file, but I want to exclude these lines from my format test of the data in between. Is there a way to isolate an awk operation to a specific range of lines in an input file? Example input:

header line
a,b,c,d
a,b,c,d
a,b,c,d
trailer line

Thank you.
 
One way...

awk '
/header/ {print $0;next}
/trailer/ {print $0;next}
{
# main bit
}
' file1
 
Try something like

/^header line$/ || /^trailer line$/ {print;next}
{
# process other lines here
}

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
If your data is structured progressively, that is if
the header lines are the first 5 you can do something
like:

Code:
awk ' {
      if (NR > 5) {
           if ($0 ~ /trailer line/} {exit}
          /*look for stuff here and print*/
          }
}' filename

 
I can't get either of these solutions to work. $EOF,,,, is my trailer data. Here is what I have:

BEGIN {FS=","}
$1 !~ mytestcriteria {print $myoutputmssg}
$2 !~ mytestcriteria {print $myoutputmssg}
$3 !~ mytestcriteria {print $myoutputmssg}
$4 !~ mytestcriteria {print $myoutputmssg}
$5 !~ mytestcriteria {print $myoutputmssg}

When I run this, $EOF,,,, is output 5 times ($1 - $5) because it doesn't match any of mytestcriteria. I'm trying to prevent the trailer from being a part of each field test. I guess I'm having trouble finding the right place for the syntax /trailer/ {print $0; next}
 
Try this

BEGIN {FS=","}
/^EOF/ {print; next}
$1 !~ mytestcriteria {print $myoutputmssg}
$2 !~ mytestcriteria {print $myoutputmssg}
$3 !~ mytestcriteria {print $myoutputmssg}
$4 !~ mytestcriteria {print $myoutputmssg}
$5 !~ mytestcriteria {print $myoutputmssg}

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
This works when I escaped the $, such as: /^\$EOF/

Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top