AWK print command for specific rows

蓝咒 提交于 2020-06-09 07:14:30

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/31165747/awk-print-command-for-specific-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!