0

I want to keep only header and trailer records in a UNIX file.

closed as too broad by Anthon, Wildcard, muru, user79743, Jakuje Feb 7 '16 at 10:28

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.

  • 1
    What have you tried? What editor/commandline-program have you been using. What didn't work? – Anthon Feb 7 '16 at 7:54
  • Filter first and last lines instead of deleting others! – Pandya Feb 7 '16 at 9:23
7

With sed:

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 -i.

4

Use head and tail, one after another:

head -n 1 file.txt; tail -n 1 file.txt

Running tail if head succeeds:

head -n 1 file.txt && tail -n 1 file.txt

Example:

% 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.