Delete last line break using sed [duplicate]

旧时模样 提交于 2019-12-02 05:42:02

问题


How to delete the last \n from a file. The file has a last blank line created for a line break in the last text line. I'm using this command:

sed '/^\s*$/d'

But that las blank line is not removed.


回答1:


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?




回答2:


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                                                                                                
a

b

c

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  \n
0000016

sed:

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

b

c

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      \n
0000015

sed:

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

b

c

2) DELETE LAST EOL CHARACTER FROM A FILE:

For this particular purpose, I would recommend using perl:

perl -pe 'chomp if eof' test
a

b

c

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.




回答3:


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

$ cat file
a
b
c

$ awk 'NR>1{print p} {p=$0}' file
a
b
c
$



回答4:


you can also use below one-liner from sed to remove the trailing blank line(s):

sed -e :a -e '/^\n*$/N;/\n$/ba'



来源:https://stackoverflow.com/questions/50559170/delete-last-line-break-using-sed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!