- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Delete last two lines
Categories
Company
Local Language
Forums
Discussions
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
08-25-2004 11:10 PM
08-25-2004 11:10 PM
I have a file in which I don't need the last two lines. How can run a command to achieve this?
I have a work around for my collegue but he is not happy with it.
sed '$d' my_file | sed '$d'
I am expecting some thing like
sed -e '$d' -e '$d' my_file.
Please help us.
Parmy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2004 11:21 PM
08-25-2004 11:21 PM
Re: Delete last two lines
Use "sed -e '$d' my_file |sed -e '$d'"
that should do it.
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2004 11:23 PM
08-25-2004 11:23 PM
Re: Delete last two lines
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2004 11:32 PM
08-25-2004 11:32 PM
Re: Delete last two lines
vi +$ my_file<
Regards
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2004 11:57 PM
08-25-2004 11:57 PM
Re: Delete last two lines
expr `wc -l my_file | cut -d" " -f1` - 2 | xargs echo | sed -e "s/^/head -/" -e "s/$/ my_file/" | sh > new_my_file
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 12:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 12:21 AM
08-26-2004 12:21 AM
Re: Delete last two lines
awk -v nb=$(wc -l my_file | cut -f1 -d" ") '{if(NR>(nb - 2)) exit; print}' my_file
Regards,
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 12:27 AM
08-26-2004 12:27 AM
Re: Delete last two lines
head $(wc -l my_file | awk '{print "-" $1 - 2}') my_file
In vi you can use the command:
$-1,$d
to delete the last two lines. Should work in ed too.
Or a complete awk solution:
awk '{ if (NR > 2) { print prev2 } prev2=prev1;prev1=$0}' < my_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 12:42 AM
08-26-2004 12:42 AM
Re: Delete last two lines
This works ..but don't know you like it or not.
# let A=`cat file1 | wc -l`-2
# head -$A file1 >file2
That works.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 01:03 AM
08-26-2004 01:03 AM
Re: Delete last two lines
I have no HP-UX where to try it but on Linux
exists the tac command (opposit to cat)
so maybe the following row will help you
tac file | sed '1,2d' | tac | tee file1
br Jan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 01:56 AM
08-26-2004 01:56 AM
Re: Delete last two lines
perl -ne 'push(@last2,$_); if (scalar @last2 > 2) { print shift @last2}'
By pushing each line onto a stack this script will not print the last 2 lines.
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 02:00 AM
08-26-2004 02:00 AM
Re: Delete last two lines
Assigning points to indicate which solution you think works the best is what helps the forum help others...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 02:13 AM
08-26-2004 02:13 AM
Re: Delete last two lines
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 02:15 AM
08-26-2004 02:15 AM
Re: Delete last two lines
Here is a one-pass in awk which does no create a temp file.
It uses a 'circular buffer' of the target 'missing line' count 'L'.
This is done with an array 'line' indexed by the current record number (NR) modulus that L.
You provide a desired value for L as argument.
We start printing from the array only after L lines have come by, 'falling behind' by L lines.
Sounds tricky? Here is the 'one line' code:
awk -vL=2 '{if(NR>L){print line[(NR-L)%L];line[NR%L]=$0}}' your-file
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 06:13 PM
08-26-2004 06:13 PM
Re: Delete last two lines
And you have a closing curly bracket wrong, resulting in missing the first lines...
So the right solution would be:
awk -vL=2 '{if(NR>L){print line[NR%L];}line[NR%L]=$0}' my_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 06:18 PM
08-26-2004 06:18 PM
Re: Delete last two lines
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 07:23 PM
08-26-2004 07:23 PM
Re: Delete last two lines
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2004 01:37 AM
08-28-2004 01:37 AM
Re: Delete last two lines
Ayup, just a touch more general purpose. I had missed the awk part of your solution.
Elmar> but my mathematic mind says you need to remove the -L in your solution.
Ayup, a simplication I overlooked this time.
Elmar> It does nothing in the end... since (NR-L)%L equals NR%L.
Ayup, other then to make the code more clear.
Elmar> And you have a closing curly bracket wrong, resulting in missing the first lines...
Ooops! Thanks for catching that.
Groetjes,
Hein.