Operating System - HP-UX
1850438 Members
3118 Online
104054 Solutions
New Discussion

Re: Scripting - cutting out a line from a file

 
SOLVED
Go to solution
Chuck J
Valued Contributor

Scripting - cutting out a line from a file

Hi

I'm using a script to create a file, but I want to also cut out the first line in the file. For example my script will create a file such as:

line 1 hello 1
line 2 hello 2
line 3 hello 3

I want the script to then go and delete the first line in the file so I end up with the following in my file:

line 2 hello 2
line 3 hello 3

Any ideas?

Chuck J
13 REPLIES 13
Justo Exposito
Esteemed Contributor

Re: Scripting - cutting out a line from a file

Hi Chuck,

For instance you can use something like that:
NUMLINES=$(wc -l yourfile)
(( NUMLINES = NUMLINES -1 ))
tail -$NUMLINES yourfile > yourfile.new

Regards,

Justo.
Help is a Beatiful word
RAC_1
Honored Contributor

Re: Scripting - cutting out a line from a file

sed '/hello 1/d' ./temple

temple is your file.
There is no substitute to HARDWORK
Robin Wakefield
Honored Contributor
Solution

Re: Scripting - cutting out a line from a file

Hi Chuck,

tail +2 file > newfile
mv newfile file

will chop the 1st line from your file.

Rgds, Robin
John Palmer
Honored Contributor

Re: Scripting - cutting out a line from a file

You could use awk, something like...

awk '{if (NR != 1) {print $0}}' filename

will print every line but the first.

Regards,
John
harry d brown jr
Honored Contributor

Re: Scripting - cutting out a line from a file


Most of the ideas posted will work, some are easier than others, and you have to remember that you will need to CREATE a NEW file.

Of course the big question I have is if you are creating the file from a script, then want the first line REMOVED, then why are you producing the first line in the original script??

live free or die
harry
Live Free or Die
Robin Wakefield
Honored Contributor

Re: Scripting - cutting out a line from a file

Hi Chuck,

perl will let you edit in-place:

perl -i -ne 'print unless ($. == 1)' filename

Rgds, Robin
Leif Halvarsson_2
Honored Contributor

Re: Scripting - cutting out a line from a file

Hi
Another awk variant:

awk 'BEGIN { getline }; { print }'
Stefan Schulz
Honored Contributor

Re: Scripting - cutting out a line from a file

Hi Chuck,

just to throw in another possible solution:

How about using ex to edit the file in place.

ex yourfile << EOF
1p
d
w
q!
EOF

This will open your file in ex, go to the first line, delete it, saves it and quits the file.

But as Harry asked, why can't you configure your script not to write this line in the first place?

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Chuck J
Valued Contributor

Re: Scripting - cutting out a line from a file

Thanks for all your replies so far, I'm still to test the ones i haven't given points to, so don't worry those points are coming!

Harry / Stefan

My script calls a vendor executable script that produces the file, my script doesn't produce the file. I don't want the first line in this file that's why i want to cut it out. Hope that answers your question

Thanks

Chuck J
James R. Ferguson
Acclaimed Contributor

Re: Scripting - cutting out a line from a file

Hi Chuck:

In my opinion the fastest way to accomplish this is with 'sed' to select everything but the first line, regardless of content. 'sed' has an advantage over 'tail' insofar as 'tail' insofar as it can process much larger files than 'tail':

# sed -e '1d' myinput > myoutput

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Scripting - cutting out a line from a file

Some other perl solutions

# perl -ne'2..eof and print' blah
# perl -ne'1..1 or print' blah
# perl -ne'$.>1 and print' blah

And a silly solution :)

# pr -n:4 blah | grep -v '^0*1:' | sed 's/^[0-9]*;//'
Enjoy, Have FUN! H.Merijn
Stefan Schulz
Honored Contributor

Re: Scripting - cutting out a line from a file

Hi again,

its just interesting how many solutions you get for one problem.

As always it depends on your situation which solution is the best for you.

If you can't influence how this file is created, you have to format it to you needs. (yes my question is answered)

I just wanted to show another way to solve this. This ex solution is pretty helpful if you need to edit a file in place. But for speed reasons i personally would prefer one of the sed solutions.

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Chia-Wei
Advisor

Re: Scripting - cutting out a line from a file

sed '1,1d' filename1 > filename2 will do the trick! ;)