Regex: Put + Between Paragraphs
I need a regex one liner for the following problem.
If There is multiple paragraph after a Line which has ::, Then have to put + between each paragraph. Current Text Code:
One dollar:: and eighty-seven cents. That was all. And sixty cents of it was in pennies. Code:
One dollar:: and eighty-seven cents. That was all. And sixty cents of it was in pennies. Code:
$ perl -pe 's/(^.*::.*\n\n)/$1\n+\n/g' regex.txt |
Try this:
Code:
sed '/\:\:/{N;s/\n$/\n+/}' |
Please let me get back to you. It might take an hour.
|
Or try reversing the pattern and changing the input record separator to something other than a new line.
Code:
perl -0x1ff -pe 's/\n\n(?!.*::)/\n+\n/g' That leaves a trailing plus on the last line, however. That might not be the most practical with very large files. You might need a more complex one-liner or even something more than just a one-liner. Edit: Code:
perl -0x1ff -pe 's/\n\n(?!.*::)(?=.)/\n+\n/g;' |
Thank you very much.
|
No problem. There is one negative lookahead (?!…) assertion and one positive (?=…) assertion. They are useful on occasion. They are non-capturing groups. Again, see "man perlre" about that.
|
Without any other options, -p processes the input line by line. No line can contain anything after the \n. You have to change the record separator:
Code:
perl -0pe 's/(::\N*\n)\n/$1+\n/g' |
@Turbocapitalist. Hats off!
@others. No, it's not so easy, see Turbocapitalist's solution. The OP didn't make it clear, but lines that don't include :: are considered part of the previous paragraph. |
Yes, now I understand.
Code:
perl -pe 'BEGIN{$/="::"} {s/(\n\N+\n)/+$1/g}' |
That looks more efficient. But that use for \N sure is buried deeply in the manual page.
The special variables also have more mnemonic names, too: Code:
perl -pe 'BEGIN{$RS="::"} {s/(\n\N+\n)/+$1/g}' Code:
perl -MEnglish -pe 'BEGIN{$INPUT_RECORD_SEPARATOR="::"} {s/(\n\N+\n)/+$1/g}' |
All times are GMT -5. The time now is 09:06 PM. |