首页 > 解决方案 > awk 中的管道如何工作(保留标头排序)

问题描述

下面的命令输出一个文件的头部并且在头部之后对记录进行排序。但它是如何工作的?谁能解释一下这个命令?

awk 'NR == 1; NR > 1 {print $0 | "sort -k3"}'

标签: shellawk

解决方案


请您看一遍(仅用于解释目的)。为了学习更多的概念,awk我建议通过Stack Overflow 不错的 awk 学习部分

awk '                    ##Starting awk program from here.
NR == 1;                 ##Checking if line is first line then print it.
##awk works on method of condition then action since here is NO ACTION mentioned so by default printing of current line will happen
NR > 1{                  ##If line is more than 1st line then do following.
 print $0 | "sort -k3"   ##It will be keep printing lines into memory and before printing it will sort them with their 3rd field.
}'

推荐阅读