- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Scripting - cutting out a line from a file
Categories
Company
Local Language
Forums
Discussions
Knowledge Base
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 02:29 AM
10-21-2002 02:29 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 02:36 AM
10-21-2002 02:36 AM
Re: Scripting - cutting out a line from a file
For instance you can use something like that:
NUMLINES=$(wc -l yourfile)
(( NUMLINES = NUMLINES -1 ))
tail -$NUMLINES yourfile > yourfile.new
Regards,
Justo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 02:44 AM
10-21-2002 02:44 AM
Re: Scripting - cutting out a line from a file
temple is your file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 02:45 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 02:45 AM
10-21-2002 02:45 AM
Re: Scripting - cutting out a line from a file
awk '{if (NR != 1) {print $0}}' filename
will print every line but the first.
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 02:50 AM
10-21-2002 02:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 02:56 AM
10-21-2002 02:56 AM
Re: Scripting - cutting out a line from a file
perl will let you edit in-place:
perl -i -ne 'print unless ($. == 1)' filename
Rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 03:06 AM
10-21-2002 03:06 AM
Re: Scripting - cutting out a line from a file
Another awk variant:
awk 'BEGIN { getline }; { print }'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 04:14 AM
10-21-2002 04:14 AM
Re: Scripting - cutting out a line from a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 05:40 AM
10-21-2002 05:40 AM
Re: Scripting - cutting out a line from a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 05:47 AM
10-21-2002 05:47 AM
Re: Scripting - cutting out a line from a file
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 05:53 AM
10-21-2002 05:53 AM
Re: Scripting - cutting out a line from a file
# 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]*;//'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 06:22 AM
10-21-2002 06:22 AM
Re: Scripting - cutting out a line from a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2002 05:04 PM
10-21-2002 05:04 PM