I want to keep only header and trailer records in a UNIX file.
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
sed -n '1p;$p' file
Suppress automatic printing of pattern space (
-n) but print first (
1p) and last line (
$p) of pattern space.
If you want to edit your file "in place" use sed's option
tail, one after another:
head -n 1 file.txt; tail -n 1 file.txt
head -n 1 file.txt && tail -n 1 file.txt
% cat file.txt foo spam egg bar % head -n 1 file.txt; tail -n 1 file.txt foo bar % head -n 1 file.txt && tail -n 1 file.txt foo bar