删除单词之间的空格并折叠Bash中的双空格

时间:2017-05-10 09:16:53

标签: bash shell text

我有以下文字:

T h i s  i s  s o m e  t e x t .

我需要的是以下内容:

This is some text.

结构遵循常规模式,因​​此我假设有一种方法可以使用shell命令执行必要的修改(也可以是某种类型的脚本)。我并不精通shell工具,所以我无法想出一些有用的东西..

提前致谢!

4 个答案:

答案 0 :(得分:3)

使用sed,你可以这样做:

$ echo "$a"
T h i s  i s  s o m e  t e x t .

$ sed 's/\(.\) /\1/g' <<< "$a"
This is some text.

答案 1 :(得分:3)

Perl救援:

perl -pe 's/ (?! )//g' -- input.txt
  • (?!是一个&#34;负面预测断言&#34;,这意味着整个模式意味着一个空格后面没有空格

答案 2 :(得分:2)

上面提到的

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> <SignedInfo> <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> <Reference URI="#_0"> <Transforms> <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> </Transforms> <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> <DigestValue>[removed]</DigestValue> </Reference> </SignedInfo> <SignatureValue>[removed]</SignatureValue> <KeyInfo> <o:SecurityTokenReference> <o:Reference URI="[removed]"/> </o:SecurityTokenReference> </KeyInfo> </Signature> </o:Security> </s:Header> <s:Body> [removed] </s:Body> </s:Envelope> sed变体是直接的方法。

perl变体为例。:

awk

答案 3 :(得分:2)

你可以使用这个基于单词边界的gnu sed:

s='T h i s  i s  s o m e  t e x t    .'

sed -E 's/\b( +\B| )//g' <<< "$s"

<强>输出:

This is some text.
相关问题