|
|||||||||
| 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 |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
Code:
#From 1 to 8
awk 'NR<=8 {print}' file
#Between 5 and 9 inclusive
awk 'NR>=5 && NR<=9 {print}' fileYou 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
|
||||
|
||||
|
Quote:
Code:
awk 'NR =9 {print}' FILENAMEgave me all the lines in the file. How can I just print one specific line according to line number then? Thanks again!! |
|
#4
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
Quote:
|
| Sponsored Links | ||
|
|
![]() |
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 |
|
|