Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK print command for specific rows

Tags:

unix

awk

I have millions of records in my file, what i need to do is print columns 1396 to 1400 for specific number of rows, and if i can get this in excel or notepad.

Tried with this command

awk {print $1396,$1397,$1398,$1399,$1400}' file_name

But this is running for each row.

like image 374
akram Avatar asked Oct 15 '25 17:10

akram


1 Answers

You need a condition to specify which rows to apply the action to:

awk '<<condition goes here>> {print $1396,$1397,$1398,$1399,$1400}' file_name

For example, to do this only for rows 50 to 100:

awk 'NR >= 50 && NR <= 100 {print $1396,$1397,$1398,$1399,$1400}' file_name

(Depending on what you want to do, you can also have much more complicated selection patterns than this.)

Here's a simpler example for testing:

awk 'NR >= 3 && NR <= 5 {print $2, $3}'

If I run this on an input file containing

1 2 3 4
2 3 4 5
3 a b 6
4 c d 7
5 e f 8
6 7 8 9

I get the output

a b
c d
e f
like image 86
Steve Summit Avatar answered Oct 18 '25 13:10

Steve Summit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!