How to Use Sed to Remove the Last N Lines of a File

How to use sed to remove the last n lines of a file

I don't know about sed, but it can be done with head:

head -n -2 myfile.txt

Remove the last line from a file in Bash

Using GNU sed:

sed -i '$ d' foo.txt

The -i option does not exist in GNU sed versions older than 3.95, so you have to use it as a filter with a temporary file:

cp foo.txt foo.txt.tmp
sed '$ d' foo.txt.tmp > foo.txt
rm -f foo.txt.tmp

Of course, in that case you could also use head -n -1 instead of sed.

MacOS:

On Mac OS X (as of 10.7.4), the equivalent of the sed -i command above is

sed -i '' -e '$ d' foo.txt

How to delete the last n lines of a file?

You can use a temporary file store the intermediate result of head -n

I think the code below should work:

 head -n -12 /var/lib/pgsql/9.6/data/pg_hba.conf  > /tmp/tmp.pg.hba.$$ && mv /tmp/tmp.pg.hba.$$ /var/lib/pgsql/9.6/data/pg_hba.conf

If you are putting it a script, a more readable and easy to maintain code would be:

 SRC_FILE=/var/lib/pgsql/9.6/data/pg_hba.conf
TMP_FILE=/tmp/tmp.pg.hba.$$
head -n -12 $SRC_FILE > $TMP_FILE && mv $TMP_FILE $SRC_FILE

I would suggest backing up /var/lib/pgsql/9.6/data/pg_hba.conf before running any script.

sed - how to delete everything but the last n lines?

From this list of sed one-liners:

sed -e :a -e '$q;N;11,$D;ba'

On Windows:

sed.exe -e :a -e "$q;N;11,$D;ba"

That example is for n = 10. Replace 11 with n + 1. It basically works by keeping a running list in the pattern space. For the first 10 lines, it just appends to the pattern space (N), then loops to the beginning. For 11 and later, it also deletes the first line of the pattern space. Finally, at the ($), it quits, which automatically prints the final n lines.

This is really not sed's strong suit, though. I would just install coreutils from gnuwin32.

how can we remove last 7 lines of file in unix

You can use head

head -n-7 file

from man page:

 -n, --lines=[-]K
print the first K lines instead of the first 10;
with the leading '-', print all but the last K lines of each file

like:

kent$ seq 10|head -n-7
1
2
3

Removing the new line in the last line of a text file using sed

This might work for you (GNU sed):

sed -z 's/\n\+$//' file

This will remove a newline(s) at the end of a file provided there are no null characters.

N.B. Normal use of sed i.e. without the -z option which slurps the file into memory, will remove any newlines before the sed commands can act on them.

remove last N lines from file bash

You can just use inline editing in sed:

sed -i '/<div class="col-wrapper">/,$d' file1.txt

to save changes to file1.txt

How to delete last n rows in a file by using bash file?

nl=`wc -l fileName | awk 's=$1-3{print s}'`; head -n $nl fileName > file_withoutlast3rows.txt

wc -l calculates the total number of lines in the file. Using awk, you can print the total number of lines -3. Then use head with -n option to read only that many lines. You can also put that many lines into a new file using >.

You can also use awk to do this :

awk -v nl=$(wc -l <fileName) 'NR<(nl-2)' fileName

remove the last n lines from all files of a certain type in the current directory, recursive

Your first snippet should work if you have bash4 and

shopt -s globstar

enabled.

globstar

If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.

See http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html



Related Topics



Leave a reply



Submit