How to delete first two lines and last four lines from a text file with bash?

I am trying to delete first two lines and last four lines from my text files. How can I do this with Bash?


Solution 1:

You can combine tail and head:

$ tail -n +3 file.txt | head -n -4 > file.txt.new && mv file.txt.new file.txt

Solution 2:

Head and Tail

cat input.txt | tail -n +3 | head -n -4

Sed Solution

cat input.txt | sed '1,2d' | sed -n -e :a -e '1,4!{P;N;D;};N;ba'