Operating System - HP-UX
1822529 Members
2601 Online
109642 Solutions
New Discussion юеВ

Re: Delete 1st 10 lines from a file

 
SOLVED
Go to solution
Muktha
Frequent Advisor

Delete 1st 10 lines from a file

Hi all,

I want to delete 1st 10 lines from a file.
Could you please help me to do so?

Regards
Muktha
16 REPLIES 16
Sandeep_Chaudhary
Trusted Contributor
Solution

Re: Delete 1st 10 lines from a file

# delete the first 10 lines of a file
cat filename|sed '1,10d' >newfile
Oviwan
Honored Contributor

Re: Delete 1st 10 lines from a file

Hi

you can use sed:
# delete the first 10 lines of a file
sed '1,10d'

check here for more handy one-liners:
http://www.student.northpark.edu/pemente/sed/sed1line.txt

regards
Muktha
Frequent Advisor

Re: Delete 1st 10 lines from a file

Hi Sandeep,Oviwan,

Thanks a lot..
I got the expected output.

Regards
Mukha
Muktha
Frequent Advisor

Re: Delete 1st 10 lines from a file

Hi all,

I am facing 1 more problem , I can't give
a variable to use sed command

For example:-
sed '1,$COUNT d' File1 > File2

Its not possible.

Could please help me resolve this?

Regards
Muktha
OFC_EDM
Respected Contributor

Re: Delete 1st 10 lines from a file

tail -10 will work as shown below.

I first show the content of a test file with 20 lines.

Then the result of the tail -10 command

Cheers

../testscripts >cat 1to20.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

../testscripts >tail -10 1to20.txt
11
12
13
14
15
16
17
18
19
20
The Devil is in the detail.
Sandeep_Chaudhary
Trusted Contributor

Re: Delete 1st 10 lines from a file

export COUNT=urvalue

sed "1,$COUNT d" file1>file2
OFC_EDM
Respected Contributor

Re: Delete 1st 10 lines from a file

Since others are give SED examples I'm giving you a different option.

Here it is in a script. Tweak to suit you needs.

#!/usr/bin/ksh

# set integer representing number of lines
# to remove
typeset -i i=10

tail -${i} 1to20.txt >> SOMEFILENAME.txt

exit
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: Delete 1st 10 lines from a file

I thought i'd offer a Perl option as well.

Slow day :)

This deletes the lines from the file. And backs it up to foo.txt.old first.

If you remove the -i option then it just deletes the lines...in the ORIGINAL file. So you're working with just ONE file and not 2 as in our previous posts. (BUT I suggest to use the -i option to back it up.)

# delete first 10 lines
perl -i.old -ne 'print unless 1 .. 10' foo.txt
The Devil is in the detail.
Muktha
Frequent Advisor

Re: Delete 1st 10 lines from a file

Thanks to all for the helping me.
Thanks a lot Sandeep .


Regards
Muktha
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