Operating System - Linux
1827452 Members
4552 Online
109965 Solutions
New Discussion

How to strip off the first and last line of an input file

 

How to strip off the first and last line of an input file

I need to strip off the first and last line of an input file and output the rest to an output file (Actually, the first line is the header, last line is the trailer and the middle lines are data records)

tail command was tried out but does'nt work with largefiles. (tail +2 input file | head –count_of_data_records > output file)
'sed' was also tried , but it has a limitation on record length (around 4000 char).

Can someone suggest an alternative for this , maybe using AWK.

Tanks in advance.
It Has Been...
7 REPLIES 7
Pete Randall
Outstanding Contributor

Re: How to strip off the first and last line of an input file

From "Handy One-Liners for Sed" (attached)

# print all but first line of file
sed 1d infile >> outfile

-and-

# delete the last line of the file
sed -e '$d' outputfile


Pete

Pete
H.Merijn Brand (procura
Honored Contributor

Re: How to strip off the first and last line of an input file

lt09:/home/merijn 104 > perl -ne'BEGIN{scalar<>}print$p;$p=$_' xx.txt
2 bah
3 ohh
4 yea
lt09:/home/merijn 105 > cat xx.txt
1 hah
2 bah
3 ohh
4 yea
5 mee
lt09:/home/merijn 106 >

or, shorter

lt09:/home/merijn 107 > perl -ne'$.>2&&print$p;$p=$_' xx.txt
2 bah
3 ohh
4 yea
lt09:/home/merijn 108 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Vibhor Kumar Agarwal
Esteemed Contributor

Re: How to strip off the first and last line of an input file

sed -e '1d' -e '$d' filename
Vibhor Kumar Agarwal
James R. Ferguson
Acclaimed Contributor

Re: How to strip off the first and last line of an input file

Hi:

Another perl variation:

# perl -ne 'print if ($. > 1 && !eof)' myfile

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: How to strip off the first and last line of an input file


Merijn's perl solution would be my favourite, but if you insist on awk a much similar solution is:


awk 'BEGIN{getline} (NR>2) {print line}{line=$0}' input-file > output-file

- eat the first line to begin
- print variable 'line', holding the prior line, starting on the 3rd input line.
- stash current input line in variable 'line'

The 'tricky' part in awk was to deal with an initially empty 'line'. Awk insists on 'helping' by adding a new-line, perl has no such problem.

I have NOT tried on long records.
hth,
Hein
Sandman!
Honored Contributor

Re: How to strip off the first and last line of an input file

Srinivas,

It can be done easily in ex with the following commands:

# ex -s input_file <<\EOF
> 1d | $d
> wq
> EOF
Sandman!
Honored Contributor

Re: How to strip off the first and last line of an input file

Srinivas,

Haven't tested this on record lengths of approx. 4000 chars, but this awk construct will strip an input file of its first and last lines:

# awk '{"wc -l <" FILENAME|getline cnt;if((NR!=1)&&(NR!=cnt)) print $0 > "outfile"}' inputfile

cheers!