Operating System - HP-UX
1826993 Members
3452 Online
109705 Solutions
New Discussion

how to cut off a file's top 4 lines

 
SOLVED
Go to solution
Werner Ittner
Occasional Advisor

how to cut off a file's top 4 lines

I need a script (awk ?) which reads a file, and writes another file which is identical to the first file but without the top 4 lines.

Thanks

Werner
7 REPLIES 7
Michael Tully
Honored Contributor
Solution

Re: how to cut off a file's top 4 lines


How about:

$ cat file1 | sed -n '4,$p' >> file2
Anyone for a Mutiny ?
john korterman
Honored Contributor

Re: how to cut off a file's top 4 lines

Hi,
a simple suggestion...

#!/usr/bin/sh
TOTAL_NO_OF_LINES=`wc -l $1 | awk '{print $1}'`
tail -"$(( $TOTAL_NO_OF_LINES - 4 ))" $1

run the above with you input file as $1.

regards,
John K.
it would be nice if you always got a second chance
V.Tamilvanan
Honored Contributor

Re: how to cut off a file's top 4 lines

Hi,

#tail +5 originalfile >newfile

HTH
Leif Halvarsson_2
Honored Contributor

Re: how to cut off a file's top 4 lines

Hi,
if you want to use awk:
cat | awk 'BEGIN { getline; getline; getline; getline} { print }'
twang
Honored Contributor

Re: how to cut off a file's top 4 lines

You can use 'sed' too, For example:

# sed -e '1,4d' file_name > new_file
T G Manikandan
Honored Contributor

Re: how to cut off a file's top 4 lines

$cat |awk 'NR >4' > new-file
Graham Cameron_1
Honored Contributor

Re: how to cut off a file's top 4 lines

Similar to above, but you can use awk on its own, don't need cat:
# awk 'NR>4' infile > outfile

The same goes for sed (slight mistake in Michaels post, for "4", read "5"):
# sed -n '5,$p' infile > outfile

Why use 2 programs when 1 will do?

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