簡體   English   中英

bash命令從文件復制特定行

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

這會將output.txt的最后3行復制到newfile.txt

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

但是bash命令將output.txt的第(last-1)^ th和(last-3)^ th行復制到newfile.txt是什么?

您可以使用以下內容

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

tail接受最后4行,而awk選擇其中的第一和第三行,如果我理解正確的話,這就是您想要的行。

請注意,在您的命令中您也不需要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