在AWK中处理输出后如何打印结束块?

在AWK中处理输出后如何打印结束块?,awk,Awk,当我执行: awk -F '|' 'BEGIN{print "head"}{print $3|"head"}END{print "bottom"}' d16.txt 我明白了: head bottom 3823 9386 7152 8137 1933 1137 3921 2585 9637 0626 相反,我想要这个: head 3823 9386 7152 8137 1933 1137 3921 2585 9637 0626 bottom 注意head命令,如果在整个awk命令之后使用它

当我执行:

awk -F '|' 'BEGIN{print "head"}{print $3|"head"}END{print "bottom"}' d16.txt
我明白了:

head
bottom
3823
9386
7152
8137
1933
1137
3921
2585
9637
0626
相反,我想要这个:

head
3823
9386
7152
8137
1933
1137
3921
2585
9637
0626
bottom

注意
head
命令,如果在整个awk命令之后使用它,我将丢失底部字符串,但无论如何它的行为不是预期的。

为了避免调用head,只需添加一个条件:

awk -F '|' 'BEGIN{print "head"}NR<=10{print $3}END{print "bottom"}' d16.txt

对于小文件,这不会有太大的影响,但在大文件上使用这种方法是值得的。

为了避免调用head,只需添加一个条件:

awk -F '|' 'BEGIN{print "head"}NR<=10{print $3}END{print "bottom"}' d16.txt

对于小文件,这不会有太大的影响,但在较大的文件上使用这种方法是值得的。

删除|“head”。它在结束前打印前10个数字。删除|头。它在结束前打印前10个数字。可能更快:
awk'BEGIN{print“head”}1;NR>=10{exit}END{print“bottom”}
这将跳过对第11行的读取。.END.可能更快:
awk'BEGIN{print“head”}1;NR>=10{exit}END{print“bottom”}
这将跳过第11.END行的读取。