Operating System - HP-UX
1831406 Members
3559 Online
110025 Solutions
New Discussion

sed to delete first 4 lines

 
kholikt
Super Advisor

sed to delete first 4 lines

I am writing a script and I need to delete the first 4 lines from the file.

I am not very familiar with SED.
abc
8 REPLIES 8
Bernhard Mueller
Honored Contributor

Re: sed to delete first 4 lines

Hi,

sed -n -e 'p5,$' filename

Regards,
Bernhard
Elmar P. Kolkman
Honored Contributor

Re: sed to delete first 4 lines

Another solution, though a bit dirty, would be:

tail -$(expr `wc -l $1` - 4) $1

Or awk:

awk '{count++; if (count > 4) { print }}' $1
Every problem has at least one solution. Only some solutions are harder to find.
kholikt
Super Advisor

Re: sed to delete first 4 lines

Hi the Sed example I think is not working
abc
Graham Cameron_1
Honored Contributor

Re: sed to delete first 4 lines

Elmar's awk solution is more complex than need be. awk counts the rows for you, in NR, and an empty statement means print, thus:

awk '(NR>4)' inputfile

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Elmar P. Kolkman
Honored Contributor

Re: sed to delete first 4 lines

Sorry Graham, you are right. I always forget that awk has some very useful internal vars.

As for the sed: the needs to be at the end, not the beginning. So it will be:
sed -n -e '5,$p'

Every problem has at least one solution. Only some solutions are harder to find.
Bernhard Mueller
Honored Contributor

Re: sed to delete first 4 lines

Hi,

sorry for the mistake; Elmar made the right correction in the sed line, "5,$p" it has to be.

Elmar, the tail solution will not work on large files with several thousand lines.

Regards,
Bernhard
Thierry Poels_1
Honored Contributor

Re: sed to delete first 4 lines

Hi,

ed filename << EOF
1,4d
w
q
EOF



regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Pete Randall
Outstanding Contributor

Re: sed to delete first 4 lines

Attaching the "Handy One Liners for Sed" for future reference - very handy!


Pete

Pete