sed用一个空格replace所有的制表符和空格

我得到了一个string,如下所示:

test.de. 1547 IN SOA ns1.test.de. dnsmaster.test.de. 2012090701 900 1000 6000 600 

现在我想用只有一个空格replacelogging之间的所有标签/空格,所以我可以很容易地使用它与cut -d " "

我尝试了以下内容:

 sed "s/[\t[:space:]]+/[:space:]/g" 

和各种各样的变数,但不能得到它的工作。 有任何想法吗?

使用sed -e "s/[[:space:]]\+/ /g"

这是一个解释:

 [ # start of character class [:space:] # The POSIX character class for whitespace characters. It's # functionally identical to [ \t\r\n\v\f] which matches a space, # tab, carriage return, newline, vertical tab, or form feed. See # https://en.wikipedia.org/wiki/Regular_expression#POSIX_character_classes ] # end of character class \+ # one or more of the previous item (anything matched in the brackets). 

为了replace,你只需要插入一个空格。 [:space:]将不会在那里工作,因为这是一个字符类的缩写,正则expression式引擎不知道要放在那里的字符。

+必须在正则expression式中转义,因为使用sed的正则expression式引擎+是一个正常的字符,而\+是一个或多个的元字符。 在掌握正则expression式的第86页上,Jeffrey Friedl在脚注中提到ed和grep使用了转义括号,因为“Ken Thompson认为正则expression式主要用于C代码,在那里需要匹配原始括号比反向引用更常见“。 我假设他对加号也有同样的感觉,所以有必要将它用作元字符。 这很容易被绊倒。

在SEED你需要逃避+ ,? , |() 。 或使用-r来使用扩展正则expression式(然后看起来像sed -r -e "s/[[:space:]]\+/ /g"sed -re "s/[[:space:]]\+/ /g"

我喜欢使用下面的别名bash。 基于别人写的东西,使用sed来search并用一个空格replace多个空格。 这有助于从剪切中获得一致的结果。 最后,我再次通过sed运行它,将空间更改为制表符,以便读取更容易。

 alias ll='ls -lh | sed "s/ \+/ /g" | cut -f5,9 -d" " | sed "s/ /\t/g"'