Simple script to delete last n lines
This may be possible using a single command but a very small and simple script though.Here we are deleting last 3 lines from a file.
#!/bin/bash
#Assign the number of lines to be deleted to variable "a"
a=3
i=1
while [ $i -le $a ]
do
sed -i '$d' file
i=$(($i + 1))
done
#!/bin/bash
#Assign the number of lines to be deleted to variable "a"
a=3
i=1
while [ $i -le $a ]
do
sed -i '$d' file
i=$(($i + 1))
done
Total Comments 2
Comments
-
how to using the script,
It's getting parameter or have to defile file path in script, or what else?
I think, you have to give a sample to understandable.
for example,
should write command like following to delete last 3 lines of file.txt
# script.sh file.txt
RegardsPosted 11-02-2013 at 12:44 PM by idogan -
Using this script i am deleting last three lines from a file
Here in this case, my script and the file in which the lines to be deleted are in the same location.
so simply run
./script or sh -x script
so that last three lines from filename called file would be deleted.
Note : the filename is mentioned in the script itself, if your file is in someother location just edit the script as
"sed -i '$d' path_to_the_file_whose_lines_to_be_deleted" and run the script.Posted 12-31-2013 at 01:22 AM by dinakumar12
Updated 12-31-2013 at 01:27 AM by dinakumar12