The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use sed modify specific lines Jenny.palmy UNIX for Dummies Questions & Answers 5 05-18-2008 03:17 AM
How to print a specific page randomcz UNIX for Dummies Questions & Answers 3 01-31-2007 11:03 AM
How to print at a specific position of the display efernandes Shell Programming and Scripting 1 01-23-2007 04:08 PM
Print lines with search string at specific position HealthyGuy Shell Programming and Scripting 4 12-04-2006 05:47 AM
Inserting specific lines in vi giannicello UNIX for Dummies Questions & Answers 2 05-01-2002 05:10 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-15-2007
Registered User
 

Join Date: Aug 2007
Posts: 4
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-15-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,441
Code:
awk 'NR==5 || NR==9' "file"
if you want a range
Code:
awk 'NR>=5&&NR<=9' "file"

Last edited by ghostdog74; 08-15-2007 at 01:38 AM.
Reply With Quote
  #3 (permalink)  
Old 08-15-2007
Registered User
 

Join Date: Jun 2007
Posts: 355
try this one

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

Code:
awk 'BEGIN{min=2;max=5}
{
 if (NR>=min)
{
if(NR<=max)
print
}
}' filename
Reply With Quote
  #4 (permalink)  
Old 08-15-2007
Registered User
 

Join Date: Aug 2007
Posts: 4
Print-outs in END

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
Reply With Quote
  #5 (permalink)  
Old 08-15-2007
Registered User
 

Join Date: Jun 2007
Posts: 355
my opinion

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
Reply With Quote
  #6 (permalink)  
Old 08-15-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,469
Could you please post a sample input and output ?
Reply With Quote
  #7 (permalink)  
Old 08-15-2007
Registered User
 

Join Date: Aug 2007
Posts: 4
Sample

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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0