Regular Expression over multiple lines

后端 未结 2 463
温柔的废话
温柔的废话 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:56

    Multiline in sed isn't necessarily tricky per se, it's just that it uses commands most people aren't familiar with and have certain side effects, like delimiting the current line from the next line with a '\n' when you use 'N' to append the next line to the pattern space.

    Anyway, it's much easier if you match on a line that starts with a comma to decide whether or not to remove the newline, so that's what I did here:

    sed 'N;/\n,/s/"\? *\n//;P;D' title_csv
    

    Input

    $ cat title_csv
    don't touch this line
    don't touch this line either
    This is a long abstract describing something. What follows is the tile for this sentence."
    ,Title1
    seriously, don't touch this line
    This is another sentence that is running on one line. On the next line you can find the title.
    ,Title2
    also, don't touch this line
    

    Output

    $ sed 'N;/\n,/s/"\? *\n//;P;D' title_csv
    don't touch this line
    don't touch this line either
    This is a long abstract describing something. What follows is the tile for this sentence.,Title1
    seriously, don't touch this line
    This is another sentence that is running on one line. On the next line you can find the title.,Title2
    also, don't touch this line
    

提交回复
热议问题