Add CRs (newlines)

I have a long file originally created with vi but at some point saved with MS Word. At another time I substituted all occurrences of ^M with XXX. Now I'd like to get this back to vi but with the XXX converted to newline.

I'm using whatever version of vim Apple employs.

Thanks,

Gale

You can try something like this:

awk '{sub("XXX","");print}' file > newfile

Regards

Thanks, I've been trying to do this in vi with:

:%s/XXX/^M/g

and all I get are the ^Ms when I wanted a carriage return.

BTW I know to use ctrl-v before the M.

In this awk line won't "" just remove each occurrence of XXX?

It removes one occurrence in a line, I suppose you have one occurrence of XXX at the end of the line?
I'm not shure I understand what you're trying to achieve. Do you want to convert the file to dos/windows format?

Regards

I thought my original message was clear.

This is one long file in a single line. Wherever XXX appears I need a carriage return or newline so the file can be read and edited with vi.

So I'm not just trying to get rid of XXX, I'm trying to REPLACE them with a carriage return.

I originally screwed up this file after it had been saved in MS Word and of course there was a ^M everywhere.

I used the substitute command in vi to get rid of those ^Ms but still didn't achieve the result I wanted. Then I substituted XXX for ^M with:

:%s/^M/XXX/g

If you still have the original file with a ^M after each line you can remove them with:

tr -d '\r' < your_file > new_file

Regards

Shell method:
awk '{gsub("XXX","\r");print}' yourfile > newfile

I searched UNIX.COM for "awk global replace" and found help in the sixth returned link.

You should try that with "vi global replace" if you are determined to do it in vi.
Use the whacky search button in the upper right corner of the page.
:slight_smile:

Did you try replacing the "^M" with "\r"?

Removal is not a problem. The only file I have has one line with >400,000 characters. Where ^M was at one time now has XXX but this is still all on one line.

I need to insert a carriage return at every instance of ^M.

tr, awk, or simply substitution with vi will work but I'm not using the proper syntax to get a carriage return or newline.

***********
Just tried a few more and one worked!!

within vi....
:%s/XXX/\r/g

changed them all.

I would not have thought of trying \r had I not seen that in your awk statement.

Thanks!

Or if OS X has dos2unix(1) then:

$ dos2unix inputfile.dos.txt > outputfile.unix.txt

would do the trick.

dos2unix for OS X seems to be available from here: Dos2unix version 3.1 - How to Download and Install on Mac OS X