How to print specific lines with awk

Hi!

How can I print out a specific range of rows, like "cat file | awk NR==5,NR==9", but in the END-statement?

I have a small awk-script that finds specific rows in a file and saves the line number in an array, like this:

awk '
BEGIN { count=0}

/ZZZZ/ {
list[count]=NR
counter++
}

END {
print "Total of:" count " rows."
for (i in list) {
print "Row: " list [i]## Here, also print the actual row with linenr:list[i].
}
}
' infile.txt

Any help would be much appreciated.
//Bugenhagen

awk 'NR==5 || NR==9' "file"

if you want a range

awk 'NR>=5&&NR<=9' "file"

I think this can help you. Suppose you want to print the line from MIN to MAX:

awk 'BEGIN{min=2;max=5}
{
 if (NR>=min)
{
if(NR<=max)
print
}
}' filename

Hi!
Thank You for answering.

I need to do the print-out in the END-statement, when NR is already at the last row.

This is because my lineNr-array is quite large and every number is the begining of a group of rows that I need to print out.

So if the array is: "120,140,166,178, ..." , I would need to decide in END how many stanzas that will be printed to a file. After that, I will take the next group of stanzas and print them to a new file.

So is it possibe to do something like:

"set NR=list[i]; print"

//Bugenhagen

Hi, i think what you want is difficult to realize.

1>the NR has gone to the last line.
2>At the END module for awk, it has gone to the end of the file, so you can not re-hold the line of the file

So,my suggestion for you is:
1> try to remember all the desired rows in first awk
2>awk again

Could you please post a sample input and output ? :slight_smile:

Hi!

Input file is from AIX/nmon.
(This is a cut-down version)
-----------------------------------------------------
AAA,progname,nmon_aix53# <-------Header starts here
AAA,command,/usr/local/bin/nmon_aix53 -f -t -s 10 -c 8640
AAA,version,v10r
AAA,build,AIX53
AAA,disks_per_line,150
BBBB,0026,hdisk31,unknown,Hitachi-HDS
BBBB,0027,hdisk32,unknown,Hitachi-HDS
BBBC,014,LV NAME LPs PPs DISTRIBUTION MOUNT POINT
BBBC,015,hd2 28 28 28..00..00..00..00 /usr
BBBP,2523,oslevel
ZZZZ,T0001,00:11:18,25-JUL-2007# <----------Body starts here
CPU01,T0001,32.9,19.0,11.7,36.5
CPU02,T0001,0.9,0.7,0.2,98.1
CPU03,T0001,53.3,10.5,5.6,30.6
CPU04,T0001,0.1,0.6,0.0,99.3
CPU05,T0001,31.2,5.1,0.7,63.1
ZZZZ,T0003,00:11:38,25-JUL-2007# <----------Next stanza in body
CPU01,T0003,44.3,4.3,0.6,50.8
CPU02,T0003,0.0,0.0,0.0,100.0
CPU03,T0003,45.2,0.0,0.0,54.8
CPU04,T0003,0.0,0.0,0.0,100.0
CPU05,T0003,19.7,0.0,0.0,80.3
BBBP,2524,ending vmstat -v# <----------------Footer starts here
BBBP,2525,ending vmstat -v," 20709370 memory pages"
BBBP,2526,ending vmstat -v," 19954046 lruable pages"
BBBP,2527,ending vmstat -v," 748541 free pages"
--------------------------------------------------------

So I would like to:
Put header in file1.
Put all stanzas (starting with ZZZZ) for a specific hour in file1.
Put footer in file1.

Next.
Put header in file2.
Put all stanzas for next hour in file2.
Put footer in file2.

Cont. until EOF.

//Br Bugenhagen

#! /opt/third-party/bin/perl

use strict;

my ($header, $i, $headerSet, %fileHash, $content, $trailer);

open(FILE, "<", "inputfile") || die "Unable to open file 'inputfile' <$!>\n";

while ( <FILE> ) {
  if ( $headerSet != 1 && !/^ZZZZ/ ) {
    $header .= $_;
  }
  elsif ( /^ZZZZ/ ) {
    $headerSet = 1;
    $fileHash{$i++} = $content if( length($content) );
    $content = "";
    $content .= $_;
  }
  elsif ( /^BBBP/ ) {
    $trailer .= $_;
  }
  else {
    $content .= $_;
  }
}
$fileHash{$i++} = $content;

close(FILE);

$fileHash{"header"} = $header;
$fileHash{"trailer"} = $trailer;

for( my $x=0; $x<$i; $x++ ) {
  open(FILE, ">", $x);
    print FILE $fileHash{"header"};
    print FILE $fileHash{$x};
    print FILE $fileHash{"trailer"};
  close(FILE);
}

exit 0

NB: this only tested with your sample.

awk 'BEGIN{c=1}
     /^AAA.+nmon_aix53#/  {print > "file"c }
     /^ZZZZ/,/^BBBP.+ending vmstat -v#/{
        if ($0~/^BBBP/){c=c+1;print >"file"c;next}
	else {	print > "file"c}
     }  
' "file"

I wonder how it worked actually.

This would not seperate the "ZZZZ" tags and footer will not be appended to the end.
Morever only first line from the header section would be appended.

Ok, Matrixmadhan, Your solution worked like a charm!

The only bad thing is that I have spent many days on this ....
Tried sed, awk.
Tried to understand lex/yacc.
Stupid, stupid me ...

Thanks a lot!