user189035
user189035

Reputation: 5789

bash command to copy specific lines from a file

This copies the last 3 lines of outputs.txt to 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?

Upvotes: 1

Views: 1025

Answers (2)

BMW
BMW

Reputation: 45243

tac command will do the trick.

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

Upvotes: 2

toth
toth

Reputation: 2552

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.

Note that in your command you don't need cat either, you can just

tail -n3 outputs.txt >> newfile.txt

Upvotes: 3

Related Questions