Regular Expression over multiple lines

后端 未结 2 457
温柔的废话
温柔的废话 2020-12-08 05:18

I\'m stuck with this for several hours now and cycled through a wealth of different tools to get the job done. Without success. It would be fantastic, if someone could help

2条回答
  •  囚心锁ツ
    2020-12-08 05:55

    Yours works with a couple of small changes:

    sed -n '1h;1!H;${;g;s/\."\?\n,//g;p;}' inputfile
    

    The ? needs to be escaped and . doesn't match newlines.

    Here's another way to do it which doesn't require using the hold space:

    sed -n '${p;q};N;/\n,/{s/"\?\n//p;b};P;D' inputfile
    

    Here is a commented version:

    sed -n '
    $          # for the last input line
    {
      p;             # print
      q              # and quit
    };
    N;         # otherwise, append the next line
    /\n,/      # if it starts with a comma
    {
      s/"\?\n//p;    # delete an optional comma and the newline and print the result
      b              # branch to the end to read the next line
    };
    P;         # it doesn't start with a comma so print it
    D          # delete the first line of the pair (it's just been printed) and loop to the top
    ' inputfile
    

提交回复
热议问题