Can't remove empty lines with sed regex

2019-05-11 22:40发布

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.

4条回答
神经病院院长
2楼-- · 2019-05-11 22:57

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'
查看更多
Emotional °昔
3楼-- · 2019-05-11 23:07

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'
查看更多
放我归山
4楼-- · 2019-05-11 23:11

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)

查看更多
三岁会撩人
5楼-- · 2019-05-11 23:13

to delete blank lines:

sed '/^[[:space:]]*$/d'
查看更多
登录 后发表回答