Bash 排序并跳过标题。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11257581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash sort and skip the header.
提问by sk8asd123
I have a script to sort respect to certain column in a for loop. I would like to skip the header in the sorting process but I'm failing. This is what I have:
我有一个脚本来对 for 循环中的某些列进行排序。我想在排序过程中跳过标题,但我失败了。这就是我所拥有的:
for i in file*
do
sort -k 1,1 -k 3,3n -t\; ${i} > h${i}
rm ${i}
done
How to skip the the header in the sorting process but keep it in the output?
如何在排序过程中跳过标题但将其保留在输出中?
Thanks.
谢谢。
采纳答案by rush
Is header the first line of the file? If it is, try next one:
header 是文件的第一行吗?如果是,请尝试下一个:
for i in file*
do
head -1 ${i} > h${i}
sed 1d ${i} | sort -k 1,1 -k 3,3n -t\; >> h${i}
rm ${i}
done
回答by sk8asd123
cat YOURFILE| (read -r; printf "%s\n" "$REPLY"; sort)
回答by tuxuday
You can remove the header before sorting using sed. untested code.
您可以在使用 sed 排序之前删除标题。未经测试的代码。
for i in file*
do
sed 1d ${i} | sort -k 1,1 -k 3,3n -t\; > h${i}
rm ${i}
done
回答by potong
This might work for you (GNU sed and Bash):
这可能对您有用(GNU sed 和 Bash):
sed -i '1{h;d};:a;$!{N;ba};s/.*/cat <<\EOF|sort -k1,1 -k3,3n -t\;\n&\nEOF/e;H;g' file*
回答by zcsunt
for i in file*
do
awk 'NR==1;NR>1{print|"sort -k 1,1 -k 3,3n -t\;"}' ${i} > $h{i}
rm ${i}
done

