English 中文(简体)
使用 sed regex 无法删除空行
原标题:Can t remove empty lines with sed regex

我有这样的文件:

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

ed /$/d 对我来说不起作用。 外部文件看上去就像文件。 它应该删除空行 。

问题回答

删除空白行:

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

不幸的是,手册说使用 [!-~] 这样的区域不安全。 但是,只要打印含有可打印字符的行, 使用 : print: 对我最终有效 :

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

由于在“空白”行中似乎有未知的非可打印字符, 您可以重写您的 sed 命令, 只显示含有可打印字符的行 :

sed -n  /[!-~]/p 

您也可以尝试:

 sed -n  /^./p 

其中只打印在线条开头至少有一个字符的线条。 (BTW sed /$/d 为我工作)





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签