[英]In Linux, how can I merge the output of two console commands into a text file?
我正在获取csv的第一行和最后10,000行,如下所示:
head workrace.csv -n 1
tail workrace.csv -n 10000
如何将输出合并到单个文本文件中? 我可以将上述命令传递到两个单独的文本文件中,然后将它们连接起来。 有没有一种方法可以不需要使用中间文本文件?
您可以在子shell中运行两个命令:
( head workrace.csv -n 1 ; tail workrace.csv -n 10000 ) > result.txt
或者,您可以使用>>
重定向运算符将内容添加到文件中:
head workrace.csv -n 1 > result.txt
tail workrace.csv -n 10000 >> result.txt
choroba未提及的其他一些选择:
F=workrace.c
{ head -n 1 $F; tail -n 10000 $F; } > result.txt # no subshell
awk 'NR==1 || NR>k-1000' k="$( wc -l < $F )" $F > result.txt
exec > result.txt # truncate result.txt and direct output of remaining commands to it
head -n 1 $F
tail -n 10000 $F
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.