Operating System - HP-UX
1754022 Members
7543 Online
108811 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