【问题标题】:Replace newlines in some lines with spaces using sed or awk使用 sed 或 awk 将某些行中的换行符替换为空格
【发布时间】:2018-05-24 04:41:48
【问题描述】:

我有一个包含一些事件的文本日志文件,我想对其进行修改以提高可读性和打印格式。

我有:

$cat myfile 
foo bar foo bar

1.
1. foo bar
1:00

10.
3. foo bar
3:02

11.
4. foo
5:01

foobar foo

11.
foobar foo
3:48

2.
foobar foo
4:18

我想要的是:

$cat myfile
foo bar foo bar

1. 1. foo bar 1:00
10. 3. foo bar 3:02
11 4. foo 5:01

foobar foo

11. foobar foo 3:48
2. foobar foo 4:18

感谢任何帮助!谢谢!

【问题讨论】:

    标签: linux bash awk sed tr


    【解决方案1】:

    通过将记录设置为段落模式

    awk -v RS=  '{$1=$1}1'
    

    你会得到

    foo bar foo bar
    1. 1. foo bar 1:00
    10. 3. foo bar 3:02
    11. 4. foo 5:01
    foobar foo
    11. foobar foo 3:48
    2. foobar foo 4:18
    

    要添加额外的空行,需要添加一些内容

    awk -v RS=  '{$1=$1; t=!/[0-9]\./; 
                  if(NR>1 && t) print ""; 
                  print; 
                  if(t) print ""}'
    

    得到

    foo bar foo bar
    
    1. 1. foo bar 1:00
    10. 3. foo bar 3:02
    11. 4. foo 5:01
    
    foobar foo
    
    11. foobar foo 3:48
    2. foobar foo 4:18
    

    【讨论】:

      【解决方案2】:

      你可以使用这个 sed

      sed ':A;$bB;N;/\n$/!bA;:B;s/\n//g;s/\(^[^0-9]*$\)/\n&\n/;1,2s/^\n//' myfile
      
      sed '
      :A
      $bB                     # jump to B if last line
      N                       # add a new line in the pattern space
      /\n$/!bA                # if this new line is empty, not return to A
      :B
      s/\n//g                 # remove all \n to get only one line
      s/\(^[^0-9]*$\)/\n&\n/  # if the line not contain number add \n before and after
      1,2s/^\n//              # remove the \n before the first line
      ' myfile
      

      【讨论】:

        【解决方案3】:

        你可以试试这个 awk 版本:

        awk '/\.$/{printf "%s ", $0; next} {print} your-file
        

        【讨论】:

          【解决方案4】:

          您能否尝试关注一下,如果这对您有帮助,请告诉我。

          awk '/^[0-9]+\./{ORS=" "} !NF{ORS="\n"};1; END{ORS="";print RS}'   Input_file
          

          输出如下。

          foo bar foo bar
          
          1. 1. foo bar 1:00
          10. 3. foo bar 3:02
          11. 4. foo 5:01
          foobar foo
          
          11. foobar foo 3:48
          2. foobar foo 4:18
          

          编辑:在此处添加非单行形式的解决方案并附上解释。

          awk '
          /^[0-9]+\./{  ##Checking condition here if any line starts from a digit(all together) and with a dot if yes then do following.
            ORS=" "     ##Setting value of ORS(output record separator) as space here.
          }
          !NF{          ##Checking if value of awk out of the box variable named NF value is NULL here, if yes then do following.
            ORS="\n"    ##Setting the value of ORS(output record separator) as new line here.
          };
          1;            ##By mentioning 1 here I am making condition TRUE here and not mentioning any action so by default print of current line will happen.
          END{
            ORS="";     ##Setting value of ORS(output field separator) as NULL here.
            print RS    ##Printing value of RS which is new line by default.
          }
          ' Input_file  ##Mentioning Input_file here.
          

          【讨论】:

            【解决方案5】:

            Perl 的救援:

            perl -l -000 -pe 's/^|$/\n/g if 2 != s/\n/ /g' -- file
            
            • -000 打开“段落模式”,以空行分隔的块读取输入
            • -l 从输入中删除输入分隔符并将其添加到输出中
            • s/\n/ /g 用空格替换所有换行符(在一个块中)并返回替换次数
            • s/^|$/\n/g 在块的开头和结尾添加换行符

            【讨论】:

              猜你喜欢
              • 2013-11-03
              • 1970-01-01
              • 2016-10-29
              • 2021-10-25
              • 2015-01-05
              • 2011-01-23
              • 2019-01-11
              • 2010-11-18
              相关资源
              最近更新 更多