问题
I have a bunch of textfile that looks like this:
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 .
"
And I want to change it such that the . "
are in a single line. The desired output should look like this:
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 . "
I've tried this but it doesn't work:
sed -i 's/.\n"\n/. "\n/g'
Can someone help me out on the correct sed command to shift the "
up?
回答1:
This is what I figured out:
sed -n '1{h;d};/^"$/{g;s/$/ "/p;n;h;d};x;p;${g;p}' input.txt
output
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 . "
回答2:
perl -00 -lpe 's/\n"$/"/mg'
produces the desired output.
回答3:
A slightly different sed
variant:
sed -n '1{h};1!{/"$/!H};/"$/{H;g;s/\.[ \n]*"$/\. "/;p;n;x}' input.txt
1 { h }
— put first line into the hold buffer1! { /"$/ !H }
— for the rest of the lines, accumulate into hold buffer if no lonely"
/"$/ { H; g; s/\.[ \n]*"$/\. "/; p; n; x }
— otherwise:H
— add to the hold bufferg
— move hold buffer to the pattern spaces/\.[ \n]*"$/\. "/
— do the replacementp
— print it outn
— read next linex
— and keep it in the hold buffer
回答4:
This might work for you:
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 ."
来源:https://stackoverflow.com/questions/10318189/sed-with-newlines-and