简体   繁体   English

bash命令从文件复制特定行

[英]bash command to copy specific lines from a file

This copies the last 3 lines of outputs.txt to newfile.txt 这会将output.txt的最后3行复制到newfile.txt

tail -n 3 "outputs.txt" | cat >> newfile.txt

but what is the bash command to copy the (last-1)^th and (last-3)^th line of outputs.txt to newfile.txt? 但是bash命令将output.txt的第(last-1)^ th和(last-3)^ th行复制到newfile.txt是什么?

You can use the following 您可以使用以下内容

tail -n 4 outputs.txt | awk 'NR ==1 || NR == 3' >> newfile.txt

tail takes the last 4 lines and awk selects the first and third of those, which if I understood correctly are the ones you want. tail接受最后4行,而awk选择其中的第一和第三行,如果我理解正确的话,这就是您想要的行。

Note that in your command you don't need cat either, you can just 请注意,在您的命令中您也不需要cat,您只需

tail -n3 outputs.txt >> newfile.txt

tac命令可以解决问题。

tac "outputs.txt" |awk 'NR==2 || NR==4' |tac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM