Can't remove empty lines with sed regex

馋奶兔 提交于 2019-12-07 12:14:32

问题


I have a file like this:

2733617     3.00    3   3

2733617 E1b1    8.00    8   16
2733617 E1b1b1b 2.00    2   4

2733617 I1  294.00  296 590
2733617 I2  1.00    1   2

2733617 I2a1    2.00    2   4

sed '/^$/d' does not work for me. Outfile looks just like infile. It should remove empty lines.


回答1:


Unfortunately the manual says that using ranges like [!-~] is not safe. However, just printing the lines that containing printable characters using :print: worked for me in the end:

sed -n '/[[:print:]]/p'



回答2:


Since there appear to be unknown non-printable characters in the "blank" lines, you could re-write your sed command to only display lines with printable characters in them:

sed -n '/[!-~]/p'



回答3:


to delete blank lines:

sed '/^[[:space:]]*$/d'



回答4:


You can also try:

 sed -n '/^./p'

which prints only lines which have at least one char at the beginning of the line. (BTW sed '/^$/d' works for me)



来源:https://stackoverflow.com/questions/10755692/cant-remove-empty-lines-with-sed-regex

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