0

我有一堆看起来像这样的文本文件:

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

4 回答 4

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— 并将其保存在保持缓冲区中
于 2012-04-25T15:54:52.510 回答
1
perl -00 -lpe 's/\n"$/"/mg'

produces the desired output.

于 2012-04-25T15:14:14.317 回答
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 . "
于 2012-04-25T14:53:43.030 回答
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 ."
于 2012-04-26T06:07:18.157 回答