Delete last line break using sed [duplicate] Delete last line break using sed [duplicate] unix unix

Delete last line break using sed [duplicate]


Why is sed printing a newline?

When you read the sed POSIX standard, then it states:

Whenever the pattern space is written to standard output or a named file, sed shall immediately follow it with a <newline>.

A bit more details can be found in this answer.

Removing the last <newline>:

  • truncate: If you want to delete just one-character from a file you can do :

    truncate -s -1 <file>

    This makes the file one byte shorter, i.e. remove the last character.

    From man resize: -s, --size=SIZE set or adjust the file size by SIZE bytes

    SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.

  • other answers can be found in How can I delete a newline if it is the last character in a file?


1) DELETE LAST EMPTY LINE FROM A FILE:

First of all, the command you are currently using will delete ALL empty and blank lines!

NOT ONLY THE LAST ONE.

If you want to delete the last line if it is empty/blank then you can use the following command:

sed '${/^[[:blank:]]*$/d}' test

INPUT:

cat -vTE test                                                                                                                  a$$b$$c$^I ^I $

OUTPUT:

sed '${/^[[:blank:]]*$/d}' test                                                                                                abc

Explanations:

  • the first $ will tell sed to do the processing only on the last line
  • /^[[:blank:]]*$/ the condition will be evaluate by sed and if this line is empty or composed only of blank chars it will trigger the delete operation on the pattern buffer, therefore this last line will not be printed
  • you can redirect the output of the sed command to save it to a new file or do the changes in-place using -i option (if you use it take a back up of your file!!!!) or use -i.bak to force sed to take a back up of your file before modifying it.

IMPORTANT:

If your file comes from Windows and contain some carriage returns (\r) this sed command will not work!!! You will need to remove those noisy characters by using either dos2unix or tr -d '\r'.

For files containing carriage returns <CR> (\r or ^M):

BEFORE FIXING THE FILE:

cat:

cat -vTE test a$$b$$c$^I ^I ^M$

od:

od -c test 0000000   a  \n  \n   b  \n  \n   c  \n  \t      \t      \r  \n0000016

sed:

sed '${/^[[:blank:]]*$/d}' test                                                                                                abc

AFTER FIXING THE FILE:

dos2unix test dos2unix: converting file test to Unix format ...

cat:

cat -vTE test                                                                                                                  a$$b$$c$^I ^I $

od:

od -c test 0000000   a  \n  \n   b  \n  \n   c  \n  \t      \t      \n0000015

sed:

sed '${/^[[:blank:]]*$/d}' test                                                                                                abc

2) DELETE LAST EOL CHARACTER FROM A FILE:

For this particular purpose, I would recommend using perl:

perl -pe 'chomp if eof' testabc

you can add -i option to to the change in-place (take a backup of your file before running the command). Last but not least, you might have to remove Carriage Return from your files as described hereover.


Your question isn't clear but this might be what you're asking for:

$ cat fileabc$ awk 'NR>1{print p} {p=$0}' fileabc$