【问题标题】:How can join consecutive non-empty lines using sed/awk?如何使用 sed/awk 连接连续的非空行?
【发布时间】:2017-09-02 23:42:03
【问题描述】:

如何使用 sed 或 awk 将连续的非空行连接成单行? 举个例子说明我正在尝试做的事情。

输入:

aaa ff gg
bbb eee eee
ss gg dd

aaa ff gg
bbb eee eee
ss gg dd

aaa ff gg
bbb eee eee
ss gg dd

转换为

aaa ff gg bbb eee eee ss gg dd

aaa ff gg bbb eee eee ss gg dd

aaa ff gg bbb eee eee ss gg dd

【问题讨论】:

    标签: string awk sed concatenation lines


    【解决方案1】:

    这可能对你有用(GNU sed):

    sed ':a;N;/\n$/!s/\n/ /;ta' file
    

    除非附加的最后一行是空的,否则用空格替换换行符并重复。否则打印并重复。

    如果你想删除空行,那么:

    sed ':a;N;/\n$/!s/\n/ /;ta;P;d' file
    

    【讨论】:

      【解决方案2】:

      一个更易读的例子,不像 Perl:

      awk '{ if ($0 == "") { print line "\n"; line = "" } else line = line $0 } END { if (line) print line }' file
      

      【讨论】:

        【解决方案3】:

        不确定您是否真的想要在每条数据行之间有一个空白行,所以这里都是:

        $ awk -v RS= '{$1=$1}1' file
        aaa ff gg bbb eee eee ss gg dd
        aaa ff gg bbb eee eee ss gg dd
        aaa ff gg bbb eee eee ss gg dd
        
        $ awk -v RS= -v ORS='\n\n' '{$1=$1}1' file
        aaa ff gg bbb eee eee ss gg dd
        
        aaa ff gg bbb eee eee ss gg dd
        
        aaa ff gg bbb eee eee ss gg dd
        

        【讨论】:

          【解决方案4】:

          @Schon:@try:

          awk '{ORS=/^$/?RS RS:FS} {$1=$1} 1;END{print RS}'  Input_file
          

          编辑:现在也添加解释。

          awk '{ 
          ORS=               ##### Setting Output field separator here.
          /^$/               ##### Checking the condition if a line starts from null.
          ?                  ##### ? means if above condition is TRUE then run following action.
          RS RS              ##### set ORS as RS RS means set it to 2 new lines, default value of RS will be new line.
          :                  ##### : is a conditional operator which will execute the action following it when condition is FALSE.
          FS}                ##### Set ORS to FS, which is field separator and it's default value is space.
          {$1=$1}            ##### Re-setting the first field again of line to reflect the new value of ORS.
          1;                 ##### making the condition as TRUE and not mentioning the action, so by default print will happen of current line.
          END
          {print RS}         ##### printing the RS value at last which is new line.
          '  Input_file      ##### Mentioning the Input_file here.
          

          【讨论】:

          【解决方案5】:

          如果perl 没问题:

          $ perl -00 -pe 's/\n(?!$)/ /g' ip.txt
          aaa ff gg bbb eee eee ss gg dd
          
          aaa ff gg bbb eee eee ss gg dd
          
          aaa ff gg bbb eee eee ss gg dd
          

          【讨论】:

            猜你喜欢
            • 2011-03-12
            • 2020-12-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多