I am trying to remove the first two lines and the last four lines from my text files. How to do it using Bash?
You can combine tail and head :
$ tail -n +3 file.txt | head -n -4 > file.txt.new && mv file.txt.new file.txt
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'
This is the fastest way I've found:
sed -i 1,2d filename
You can invoke the ex editor from the bash command line using the following example. Note that a document is used here to complete the list of ex commands.
ex text.file << EOF 1,2d $ -3,.d x EOF