2

My file arrives on a Linux server and contains ^M characters when seen in VI and the file format = unix, (ff=unix)

I want to turn lines 1-4 into line lines 9 - 10. enter image description here

My vi macro solves my problem. /\r/\rxJ

It finds and removes the ^M and then joins the line to previous line.

How can I accomplish this in sed, awk or some Unix utility?

3

Try any of these:

awk -vORS= '{print sub(/\r$/,"") ? $0" " : $0"\n"}' < infile > outfile

awk 'ORS = sub(/\r$/,"") ? " " : "\n"' < infile > outfile

perl -pe 's/\r\n/ /' < infile > outfile

or in place:

perl -pe 's/\r\n/ /' -i file

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.