Operating System - HP-UX
1751840 Members
5299 Online
108782 Solutions
New Discussion юеВ

Re: Deleting last three lines in a script?

 
Vijaya Kumar_3
Respected Contributor

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

Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
2 REPLIES 2
Vijaya Kumar_3
Respected Contributor

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
}
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
Pete Randall
Outstanding Contributor

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