3

I am having a sed replacement issue and am hoping one of you will be able to help me. I am sure I am missing something simple.

So I have a file containing text and a quote. The quote itself could be on one line or span multiple lines. I want the quote on a new line by itself. As an example here is an example of the file

And he said "This too
   shall pass"

I need to change this to

And he said 
"This too shall pass"

I tried the following sed but it didn't work -- it seems to match alright but failed to get a new line

/"This/ {
    N
    s/"This *\n*too *\n*shall *\n*pass"/\n"This too shall pass"/
}
user294280
  • 351
  • 1
  • 5
  • 11

3 Answers3

3

Try replacing the "\n" with \ and an explicit new line; like the following --

/"This/ {
N
s/"This *\n*too *\n*shall *\n*pass"/\
"This too shall pass"/
}
Sai
  • 3,819
  • 1
  • 25
  • 28
2

First join all lines, then insert a newline for the first " and then a pass through tr to single space everything (not that happy with the tr part...)

$ sed -e :a -e '$!N; s/\n/ /; ta ; s/"/\n"/' multiline_input | tr -s " "
And he said 
"This too shall pass"
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
2
perl -0777 -ne 's/\s+/ /g;s/"/\n"/;print "$_\n"'
clt60
  • 62,119
  • 17
  • 107
  • 194
  • Sorry, my bad! was aiming for the up-arrow but I missed and accidentally downvoted instead. I blame my dodgy mosepad. – Fredrik Pihl Jun 16 '11 at 09:25