HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Deleting last three lines in a script?
Operating System - HP-UX
1828052
Members
1620
Online
109974
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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-18-2005 09:44 PM
08-18-2005 09:44 PM
Deleting last three lines in a script?
Hi
I need to delete last three lines of a file inside an shell script. I am trying to use this syntax but this fails:
$ sed -e '$-2,$d' myfile
Unrecognized command: $-3,$d
$
Why? Any solutions? I will assign points!
Regards
Vijay Chinnasamy
I need to delete last three lines of a file inside an shell script. I am trying to use this syntax but this fails:
$ sed -e '$-2,$d' myfile
Unrecognized command: $-3,$d
$
Why? Any solutions? I will assign points!
Regards
Vijay Chinnasamy
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2005 10:05 PM
08-18-2005 10:05 PM
Re: Deleting last three lines in a script?
From comp.unix.shell Frequently Asked Questions (FAQ) list:
19. how do I remove the last n lines?
First we need to tell the code how many lines we want to cut
from the bottom of a file.
X=10
Then We can do this:
head -n $(( $(wc -l < file ) - $X )) file >$$ \
&& cat $$ >file && rm $$
The break down:
1) $(wc -l < file)
Find out how many lines are in the file. Need to use
redirection so wc won't print the file name.
2) $(( $lines_in_file - $X ))
Take the output from step one and do some math to find out
how many lines we want to have when all is said and done.
3) head -$lines_when_said_and_done file
extracts all but the unwanted lines from the file,
and >$$ puts those lines into a temp file that has
the name of the pid of the current shell.
4) && cat $$ > file
if everything has worked so far then cat the temp file into
the original file. This is better than mv or cp because it
insures that the permissions of the temp file do not
override with the perms of the original file.
5) && rm $$
Remove the temp file.
AWK solutions:
awk 'NR<=(count-12)' count="`awk 'END{print NR}' file`" file
awk 'NR>n{print a[NR%n]} {a[NR%n]=$0}' n=12 file
awk 'BEGIN{n=12} NR>n{print a[NR%n]} {a[NR%n]=$0}' file
Whenever a line is read, the line that came 12 lines ago is
printed, and then overwritten with the newly read line, using an
rolling array indexed 0..11.
See also question 26. for information about setting awk
variables on the command line.
$SHELL/sed/mv solutions:
L=`wc -l DL=`expr $L - 11`
sed "$DL,\$d" file
L=`wc -l DL=`expr $L - 12`
sed "${DL}q" file
sed "`expr \`wc -l
sed -n -e :a -e '1,12{N;ba' -e '}' -e 'P;N;D' file
The last solution is basically same algorithm as the rolling
array awk solutions, and shares with them the advantage that
the file is only read once - they will even work in a
pipe. There may be limitations in sed's pattern space which
would make this unusable however.
PERL solution:
perl -ne' print shift @x if @x == 12; push @x, $_ ' file
Using GNU dd:
ls -l file.txt | {
IFS=" "
read z z z z sz z
last=`tail -10 file.txt | wc -c`
dd bs=1 seek=`expr $sz - $last` if=/dev/null of=file.txt
}
19. how do I remove the last n lines?
First we need to tell the code how many lines we want to cut
from the bottom of a file.
X=10
Then We can do this:
head -n $(( $(wc -l < file ) - $X )) file >$$ \
&& cat $$ >file && rm $$
The break down:
1) $(wc -l < file)
Find out how many lines are in the file. Need to use
redirection so wc won't print the file name.
2) $(( $lines_in_file - $X ))
Take the output from step one and do some math to find out
how many lines we want to have when all is said and done.
3) head -$lines_when_said_and_done file
extracts all but the unwanted lines from the file,
and >$$ puts those lines into a temp file that has
the name of the pid of the current shell.
4) && cat $$ > file
if everything has worked so far then cat the temp file into
the original file. This is better than mv or cp because it
insures that the permissions of the temp file do not
override with the perms of the original file.
5) && rm $$
Remove the temp file.
AWK solutions:
awk 'NR<=(count-12)' count="`awk 'END{print NR}' file`" file
awk 'NR>n{print a[NR%n]} {a[NR%n]=$0}' n=12 file
awk 'BEGIN{n=12} NR>n{print a[NR%n]} {a[NR%n]=$0}' file
Whenever a line is read, the line that came 12 lines ago is
printed, and then overwritten with the newly read line, using an
rolling array indexed 0..11.
See also question 26. for information about setting awk
variables on the command line.
$SHELL/sed/mv solutions:
L=`wc -l
sed "$DL,\$d" file
L=`wc -l
sed "${DL}q" file
sed "`expr \`wc -l
sed -n -e :a -e '1,12{N;ba' -e '}' -e 'P;N;D' file
The last solution is basically same algorithm as the rolling
array awk solutions, and shares with them the advantage that
the file is only read once - they will even work in a
pipe. There may be limitations in sed's pattern space which
would make this unusable however.
PERL solution:
perl -ne' print shift @x if @x == 12; push @x, $_ ' file
Using GNU dd:
ls -l file.txt | {
IFS=" "
read z z z z sz z
last=`tail -10 file.txt | wc -c`
dd bs=1 seek=`expr $sz - $last` if=/dev/null of=file.txt
}
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2005 10:10 PM
08-18-2005 10:10 PM
Re: Deleting last three lines in a script?
This is how "Handy One Liners for SED" (attached) prints the last 10 lines - you should be able to adapt this:
sed -e :a -e '$q;N;11,$D;ba'
Pete
Pete
sed -e :a -e '$q;N;11,$D;ba'
Pete
Pete
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Support
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP