【问题标题】:Replacing newline character [duplicate]替换换行符[重复]
【发布时间】:2014-06-25 01:28:37
【问题描述】:

我有一个 XML 文件,它偶尔会分成 2 行:第一行以 
 结尾。我想连接任何这样的行并删除
,也许用空格替换它。

例如

<message>hi I am&#13;
here </message>

需要成为

<message>hi I am here </message>

我试过了:

sed -i 's/&#13;\/n/ /g' filename

没有运气。

非常感谢任何帮助!

【问题讨论】:

标签: linux sed newline


【解决方案1】:

你可以使用这个 awk:

awk -F"&#13;" '/&#13;$/ {a=$1; next} a{print a, $0; a=""; next} 1' file

说明

  • -F"&amp;#13;"&amp;#13; 设置为分隔符,这样第一个字段将始终是字符串的所需部分。
  • /&amp;#13;$/ {a=$1; next} 如果该行以&amp;#13; 结尾,则将其存储在a 中并跳转到下一行。
  • a{print a, $0; a=""; next} 如果设置了a,则与当前行一起打印。然后为将来的循环取消设置 a。最后跳到下一行。
  • 1 为真,打印当前行。

样本

$ cat a
yeah
<message>hi I am&#13;
here </message>
hello
bye

$ awk -F"&#13;" '/&#13;$/ {a=$1; next} a{print a, $0; a=""; next} 1' a
yeah
<message>hi I am here </message>
hello
bye

【讨论】:

    【解决方案2】:

    试试这个笨拙的单线:

    awk -v RS="" 'gsub(/&#13;\n/," ")+7' file
    

    用你的例子在这里测试过:

    kent$ echo "<message>hi I am&#13;
    here </message>"|awk -v RS="" 'gsub(/&#13;\n/," ")+7'  
    <message>hi I am here </message>
    

    【讨论】:

      【解决方案3】:

      这对你有用:

      sed -i '{:q;N;s/&.*\n/ /g;t q}' <filename>
      

      但是,用 sed 替换换行符始终是一个 bash(读得不好)的主意。出错的几率很高。

      另外一个更简单的解决方案:

      tr -s '\&\#13\;\n' ' ' < <filename>
      

      tr 正在用空格替换所有匹配的字符,所以如果没有 -s 它会打印出来

      <message>hi I am      here </message>
      

      -s 来自手册页:

         -s, --squeeze-repeats
                replace  each  input  sequence of a repeated character that is listed in SET1 with a single occurrence of that character.
      

      【讨论】:

        【解决方案4】:

        这是一个 GNU sed 版本:

        sed ':a;$bc;N;ba;:c;s/&#13;\n/ /g' file
        

        说明:

        sed '
            :a              # Create a label a
            $bc             # If end of file then branch to label c
            N               # Append the next line to pattern space
            ba              # branch back to label a to repeat until end of file
            :c              # Another label c
            s/&#13;\n/ /g   # When end of file is reached perform this substitution
        ' file
        

        【讨论】:

        • 这在我对我的文件运行 dos2unix 后起作用。感谢大家的帮助。
        猜你喜欢
        • 2010-11-29
        • 1970-01-01
        • 2021-02-02
        • 2011-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-06
        相关资源
        最近更新 更多