sed with newlines and“

时间:2012-04-25 14:51:42

标签: regex sed newline

我有一堆文本文件,如下所示:

His doctor attributed this to an allergy .

That hardly convinced him , as he had no history of allergies of any kind .
" Yet , that was to be the least of his problems .
I may have to take steroids for the rest of my life .
"
A topical steroid spray was later added to his repertoire of drugs and 
" he knew it was merely masking the underlying condition .
"

我想改变它,使. "在一行中。所需的输出应如下所示:

His doctor attributed this to an allergy .

That hardly convinced him , as he had no history of allergies of any kind .
" Yet , that was to be the least of his problems .
I may have to take steroids for the rest of my life . "
A topical steroid spray was later added to his repertoire of drugs and 
" he knew it was merely masking the underlying condition . "

我试过这个,但它不起作用:

sed -i 's/.\n"\n/. "\n/g'

有人可以帮我找出正确的sed命令来移动"吗?

4 个答案:

答案 0 :(得分:1)

这就是我想到的:

sed -n '1{h;d};/^"$/{g;s/$/ "/p;n;h;d};x;p;${g;p}' input.txt

输出

His doctor attributed this to an allergy .

That hardly convinced him , as he had no history of allergies of any kind .
" Yet , that was to be the least of his problems .
I may have to take steroids for the rest of my life . "
A topical steroid spray was later added to his repertoire of drugs and
" he knew it was merely masking the underlying condition . "

答案 1 :(得分:1)

perl -00 -lpe 's/\n"$/"/mg'

产生所需的输出。

答案 2 :(得分:1)

略有不同的sed变体:

sed -n '1{h};1!{/"$/!H};/"$/{H;g;s/\.[ \n]*"$/\. "/;p;n;x}' input.txt
  • 1 { h } - 将第一行放入保留缓冲区
  • 1! { /"$/ !H } - 对于其他行,如果没有孤独"
  • ,则会累积到保留缓冲区中
  • /"$/ { H; g; s/\.[ \n]*"$/\. "/; p; n; x } - 否则:

    1. H - 添加到保留缓冲区
    2. g - 将保留缓冲区移动到模式空间
    3. s/\.[ \n]*"$/\. "/ - 替换
    4. p - 打印出来
    5. n - 阅读下一行
    6. x - 并将其保留在保留缓冲区中

答案 3 :(得分:0)

这可能对您有用:

sed ':a;$!N;s/\.\n"/."/;P;D' /tmp/a
His doctor attributed this to an allergy .

That hardly convinced him , as he had no history of allergies of any kind ." Yet , that  was to be the least of his problems .
I may have to take steroids for the rest of my life ."
A topical steroid spray was later added to his repertoire of drugs and 
" he knew it was merely masking the underlying condition ."