How to print specific lines with awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

How to print specific lines with awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-15-2007
Bugenhagen Bugenhagen is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 28 August 2009, 2:01 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 08-15-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts

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 04:38 AM..
Sponsored Links
    #3  
Old 08-15-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts
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

    #4  
Old 08-15-2007
Bugenhagen Bugenhagen is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 28 August 2009, 2:01 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #5  
Old 08-15-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 23 July 2014, 3:17 AM EDT
Location: Beijing China
Posts: 1,294
Thanks: 0
Thanked 24 Times in 24 Posts
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
Sponsored Links
    #6  
Old 08-15-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Could you please post a sample input and output ?
Sponsored Links
    #7  
Old 08-16-2007
Bugenhagen Bugenhagen is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 28 August 2009, 2:01 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print Specific lines when found specific character attila Shell Programming and Scripting 4 01-26-2012 04:33 AM
Problems to print specific lines with awk and grep...HELP! fedora2011 Shell Programming and Scripting 1 08-01-2011 10:37 AM
print first few lines, then apply regex on a specific column to print results. kchinnam Shell Programming and Scripting 4 08-24-2010 03:24 PM
Sed one-liner to print specific lines? Ilja Shell Programming and Scripting 2 02-12-2009 08:23 AM
print specific lines littleboyblu Shell Programming and Scripting 4 02-03-2009 07:35 AM



All times are GMT -4. The time now is 09:55 PM.