Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

unix and linux commands - unix shell scripting

AWK print line no.x to line no.y

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-09-2011   -   Original Discussion by cristalp
cristalp's Unix or Linux Image
cristalp cristalp is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 February 2012, 12:29 PM EST
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
AWK print line no.x to line no.y

I am learning AWK and quite fresh now.

My Q: How could I use AWK to print lines from e.g. first to 8th, or 5th to 9th?

Thanks!!!
Sponsored Links
    #2  
Old Unix and Linux 09-09-2011   -   Original Discussion by cristalp
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 19 October 2016, 5:02 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts


Code:
#From 1 to 8
awk 'NR<=8 {print}' file

#Between 5 and 9 inclusive
awk 'NR>=5 && NR<=9 {print}' file

You can use FNR if you have multiple files.
NR is the total number of records for all the files.
FNR is specific to each file bring processed. FNR will be reset when a new file comes in.

Try this code to find the difference


Code:
awk '{print NR "-" FNR}' file1 file2

--ahamed
The Following User Says Thank You to ahamed101 For This Useful Post:
cristalp (09-15-2011)
Sponsored Links
    #3  
Old Unix and Linux 09-09-2011   -   Original Discussion by cristalp
cristalp's Unix or Linux Image
cristalp cristalp is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 February 2012, 12:29 PM EST
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ahamed101 View Post


Code:
#From 1 to 8
awk 'NR<=8 {print}' file

#Between 5 and 9 inclusive
awk 'NR>=5 && NR<=9 {print}' file

You can use FNR if you have multiple files.
NR is the total number of records for all the files.
FNR is specific to each file bring processed. FNR will be reset when a new file comes in.

Try this code to find the difference


Code:
awk '{print NR "-" FNR}' file1 file2

--ahamed
Thanks Ahamed!! It works. But then, if I just want to print one line, say 9th line.


Code:
awk 'NR =9 {print}' FILENAME

gave me all the lines in the file. How can I just print one specific line according to line number then?

Thanks again!!
    #4  
Old Unix and Linux 09-09-2011   -   Original Discussion by cristalp
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Quote:
Originally Posted by cristalp View Post
...

Code:
awk 'NR =9 {print}' FILENAME

gave me all the lines in the file. How can I just print one specific line according to line number then?
...
You have to compare and not assign.



Code:
$
$
$ cat f20
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
$
$ awk 'NR==9 {print}' f20
This is line 9
$
$
$ awk 'NR=9 {print}' f20
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
$
$

Comparison is true only for line 9, hence it prints only line 9.
Assignment is true for each line, hence it prints each line.

tyler_durden
The Following User Says Thank You to durden_tyler For This Useful Post:
cristalp (09-15-2011)
Sponsored Links
    #5  
Old Unix and Linux 09-15-2011   -   Original Discussion by cristalp
cristalp's Unix or Linux Image
cristalp cristalp is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 February 2012, 12:29 PM EST
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by durden_tyler View Post
You have to compare and not assign.



Code:
$
$
$ cat f20
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
$
$ awk 'NR==9 {print}' f20
This is line 9
$
$
$ awk 'NR=9 {print}' f20
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
$
$

Comparison is true only for line 9, hence it prints only line 9.
Assignment is true for each line, hence it prints each line.

tyler_durden
Thanks Tyler. Now I understand. Thanks for the excellent explanation!
Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read file line by line print column wise rocking77 Shell Programming and Scripting 2 12-07-2010 08:02 AM
Line by Line Comparision of 2 files and print only the difference naveen@ Shell Programming and Scripting 1 05-19-2010 12:19 AM
How to print the words in the same line with space or to the predefined line? jobycxa Shell Programming and Scripting 5 02-18-2010 08:20 AM
Print selection of line based on line number mohanm Shell Programming and Scripting 3 02-15-2010 04:28 AM
Compare multiple fields in file1 to file2 and print line and next line gillesc_mac Shell Programming and Scripting 7 03-16-2009 07:26 AM



All times are GMT -4. The time now is 03:38 PM.