[英]Bash script: how to skip a header line when sorting a text or csv file, while retaining it in the output
(Adapted from this: Join Manual, Header lines ). (改编自: 加入手册,Header 行)。
I am sorting a file called file1 with this content:我正在使用以下内容对名为 file1 的文件进行排序:
Name Age
Charlie 34
Alice 25
If I just write:如果我只是写:
sort -k2b,2 file1
I get:我得到:
Alice 25
Charlie 34
Name Age
I can exclude the header from the sort like so:我可以像这样从排序中排除 header:
head -1 file1;(sed -n '2,$p' file1|sort -k2b,2)
But the example in the gnu manual is this:但是 gnu 手册中的示例是这样的:
( sed -u 1q; sort -k2b,2 ) < file1
Why does that work?为什么这样行得通?
I would think that I would get this instead from the command-line:我想我会从命令行得到这个:
Name Age
Alice 25
Charlie 34
Name Age
The sed consumes the first line of stdin, then the sort consumes the rest? sed 消耗 stdin 的第一行,然后排序消耗 rest?
You could have sed print the header, then sort the rest.您可以让 sed 打印 header,然后对 rest 进行排序。
sed -n '1p' file1; sort <(sed '1d' file1)
Name Age
Alice 25
Charlie 34
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.