Operating System - HP-UX
1753800 Members
7948 Online
108805 Solutions
New Discussion юеВ

Re: Delete 1st 10 lines from a file

 
SOLVED
Go to solution
Muktha
Frequent Advisor

Re: Delete 1st 10 lines from a file

I got the solution .
Thanks to all....
Frank de Vries
Respected Contributor

Re: Delete 1st 10 lines from a file

Sorry to reply late,

just a usefull tip.

You were nearly there with your sed
command, this won't work

sed '1,$COUNT d' File1 > File2

but if you use double quotes

sed "1,$COUNT d" File1 > File2



Sed is wonderfull;






Look before you leap
Dennis Handly
Acclaimed Contributor

Re: Delete 1st 10 lines from a file

>Frank: just a useful tip.

Didn't Sandeep already mention the quoting issue? :-)

>Kevin: tail -10 will work as shown below.
>set integer representing number of lines to remove

This is the wrong option to skip 10 lines and only works for your magic number, 20 lines. This requires you to do complex arithmetic operations in your head and know the number of lines in the file. Instead you should use this tail "+" option:
tail +$(( 10 + 1 )) 1to20.txt
Fredrik.eriksson
Valued Contributor

Re: Delete 1st 10 lines from a file

>This is the wrong option to skip 10 lines >and only works for your magic number, 20 >lines. This requires you to do complex >arithmetic operations in your head and know >the number of lines in the file. Instead >you should use this tail "+" option:
>tail +$(( 10 + 1 )) 1to20.txt

You could do something like this instead of doing math ;) (in script format)
#!/bin/bash
file=1to200.txt # could use $1
offset=10 # could use $2
tail -n$((`wc -l $file` - $offset)) $file > output.file

It's untested since I don't have any machine to test it.

Best regards
Fredrik Eriksson
Dennis Handly
Acclaimed Contributor

Re: Delete 1st 10 lines from a file

>Fredrik: You could do something like this instead of doing math ;)

I don't know why you would want to do this?
It reads the file twice and doesn't work for large files due to a limitation for HP's tail -N.

>It's untested

Here is a version that corrects it:
tail -n$(($(wc -l < $file) - $offset)) $file > output.file
Fredrik.eriksson
Valued Contributor

Re: Delete 1st 10 lines from a file

Well if you have large files it's obviously diffrent. Reason why I wrote it like that is because you usually don't know the exact line count, this method is sure to just remove the top 10 lines.
But you're right, tail does have a limit. I just haven't reached it yet :P

And in my first thought I did use $(wc -l) but thought that maybe (of some odd reason) $(()) didn't like me using that ;P
Dennis Handly
Acclaimed Contributor

Re: Delete 1st 10 lines from a file

>Fredrik: because you usually don't know the exact line count, this method is sure to just remove the top 10 lines.

You do NOT need to know the line count to remove the top 10 lines. You use:
tail +11 file

Similar to sed's:
sed -n '11,$p' file