[Unix] Remove Header And Trailer Records In File


[How To] Remove Header And Trailer Records In File

sed is a powerful editor and it does come to our rescue when we wish to remove the header and trailer records in a file as well!

sed command to remove the header record (first record in the file)
$ sed -i '1 d' filename

The above is a simple command to remove first line. Generalizing, we can remove the first 'n' lines of the file by below command.
$ sed -e '1,2d' filename #removes first 2 lines
$ sed -e '1,3d' filename #removes first 3 lines
$ sed -e '1,4d' filename #removes first 4 lines
and likewise ...

Note: For some flavors of Unix, sed -i may not work, please use sed -e instead.

sed command to remove the trailer record (trailer/last record in the file)
$ sed -i '$ d' filename

Note: For some flavours of Unix, sed -i may not work, please use sed -e instead.