Go to ...

Techpository

A Better Technical Repository

RSS Feed

Linux: bash/sed delete the first and last lines of a group of files


When wanting to clean up some files for testing I needed to remove code from the front and end of the file in order to focus on the information in the body of the file.
The below commands are great when cleaning up files that have header and footer information that is similar in each file.

The following example shows how to delete the first 200 lines of a file:
find -type f -name ‘*.html’ -exec sed -i -e 1,200d {} \;

The following example shows how to delete the last 155 lines of a file:
find -type f -name ‘*.html’ -exec sed -i -e :a -e ‘$d;N;2,155ba’ -e ‘P;D’ {} \;

By: Tim Conrad