关于AWK截取指定行的内容的操作,有多种实现,下面一一列举:
一、

[root@node2 script]# vim me
[root@node2 script]# cat me
i am pig
you are king
yes sir
hello dolly
[root@node2 script]#
[root@node2 script]# cat me | awk ‘{if(NR==2) print $0}’
you are king
[root@node2 script]# awk ‘{if(NR==2) print $0}’ me
you are king
[root@node2 script] 这里,如果是从第二行到最后,可以在if条件语句中做出相应的修改,如下:

[root@node2 script]# awk ‘{if(NR>=2) print $0}’ me
you are king
yes sir
hello dolly
[root@node2 script]#
二、

[root@node2 script]# awk ‘NR~/^[2]$/’ me
you are king
[root@node2 script]#
三、

[root@node2 script]# awk -v line=2 ‘NR==line’ me
you are king
[root@node2 script]#
四、

[root@node2 script]# n=3
[root@node2 script]# echo $n
3
[root@node2 script]# cat me
i am pig
you are king
yes sir
hello dolly
[root@node2 script]# awk ‘{if(NR>=’$n’) print $0}’ me
yes sir
hello dolly
[root@node2 script]#
——
To be continue。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

隐藏
变装