0

I have a file that I am busy trying to correct that has some dodgy Line Breaks inside of it, this is a small example

Row ('Row 01') Property ('Property 01')
Row ('Row 02') Property ('Property
02')
Row ('Row 03') Property ('Property 03')

So the second line has a Line Break in the value for Property which should just be one line like the first and last line.

I can correct it by doing the following in VIM

Remove all Line Breaks everywhere
:%s/\n/ /g

Which gives me

Row ('Row 01') Property ('Property 01') Row ('Row 02') Property ('Property 02') Row ('Row 03') Property ('Property 03')

Then

Reinsert new Line Breaks only where they are wanted
:%s/ Row/\rRow/g

Which gives the data that I want

Row ('Row 01') Property ('Property 01')
Row ('Row 02') Property ('Property 02')
Row ('Row 03') Property ('Property 03')

The problem is that removing every single Line Break cannot happen on a file with thousands of lines because it is terribly slow.

I'm wondering if there isn't a better VIM search and replace way of handling this without collapsing everything into a single line initially

0
gg!Gperl -0 -p -e 's/\n/ /mg; s/ Row/\nRow/mg; s/ \Z/\n/'<enter>
  • gg - go to start of file
  • !G pipe from current pos to end of file through:
  • perl -0 -p -e '....'

vim replaces the entire buffer with the output of the perl script. It never sees the intermediate stage (with no newlines) and never has to deal with absurdly long lines.

The final statement s/ \Z/\n replaces the space at the end of the file (converted from a \n by the first statement) so that it's a proper text file again (i.e. the final line ends with a newline char), without changing any other spaces that might be immediately before a newline. Alternatively, you could go to the end of the file and manually add the newline.

Better yet, do this outside of vi. save and exit, then from the shell run:

perl -i.bak -0 -p -e 's/\n/ /mg; s/ Row/\nRow/mg; s/ \Z/\n/' filename.txt

This will modify the file "in-place" and keep a backup copy of the original with a filename "extension" of .bak

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.