March 14

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


Copyright 2021. All rights reserved.

Posted March 14, 2017 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B