Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
vi \ ex : Substitute with newline?
Name: caffiene Date: September 22, 2004 at 08:02:40 Pacific OS: Windows 2000 Pro SP3 CPU/Ram: P3 600Mhz/256MB
Comment:
Is there a way to use the substitute command in vi to insert carriage returns? I'm looking at a line of data like this:
DATA1 DATA2 DATA3 DATA4 DATA5 ... so on
I want to break up each DATA entry into separate lines, like so:
DATA1 DATA2 DATA3 DATA4 ... so on
I was thinking I could substitute the spaces for carriage returns, but I do not know of a way to insert carriage returns with vi.
Name: Jim Boothe Date: September 22, 2004 at 11:59:17 Pacific
Reply:
:g/DATA/s/ [ ]*/CtrlVCtrlM/g
For all lines containing the string DATA, the above command will change each string of spaces to a newline character. The CtrlVCtrlM are two control characters in a row: Ctrl-V and Ctrl-M
That would include one or more spaces at the end of the line as well. If you do not want trailing spaces to be converted to a newline, first remove them with:
:g/DATA/s/[ ]*$
0
Response Number 2
Name: caffiene Date: September 22, 2004 at 12:51:01 Pacific
Reply:
Thank you Jim! That works like a charm. I did not realize there were special characters like that that could be put into the substitute command.
Summary: Many versions of vi/ex return this particular warning message. You probably would be better off (and safer) using the sed utility i.e. sed -f vi.sed data.txt > data.out where vi.sed contains s/...
Summary: Or vi/ex/ed with a macro script, e.g: vi myfile.dat < myscript.vi Put the vi commands in myscript.vi, including the ':'. Remember to end with ':wq'. Or use ex in place of vi - slightly leaner & mea...
Summary: vi won't replace newlines - they are treated differently from other characters. You could try variations of the "Join" command, e.g: :%j or constructions like: :g/x$/-2j (which would find all lines en...