Operating System - HP-UX
1833452 Members
3246 Online
110052 Solutions
New Discussion

^M won't print or display, except in vi command.

 
SOLVED
Go to solution
Vince Arends
Frequent Advisor

^M won't print or display, except in vi command.

I received a file from a co-worker that has ^M (control M ?) as the last character on some of the lines. When I vi the file, they show up. When I more, lp or pr the file, they don't show up. How can I get the ^M to print?
12 REPLIES 12
John Palmer
Honored Contributor

Re: ^M won't print or display, except in vi command.

^M is a character, this file must have originated from windows.

It will be printing but doesn't effectively do anything on a printer which considers to be .

You don't need the characters in UNIX, look at 'man dos2ux' which is a utility for removing them.
Vince Arends
Frequent Advisor

Re: ^M won't print or display, except in vi command.

The file is used to ftp another file to another system. If I remove the ^M from the end of the line that contains EOF, then the rest of the script does not execute. The fact that they are there does not bother me, I just want them to print. So I guess now my question is, how can I print a carriage return character?
John Palmer
Honored Contributor

Re: ^M won't print or display, except in vi command.

The character will be printed - it's just that you can't see it. It causes the printer to 'return to the start of the line' sort of equivalent to the 'Home' key in a Windows editor.

It's only UNIX utiliies like vi that show it up as ^M.
John Palmer
Honored Contributor

Re: ^M won't print or display, except in vi command.

If you'd care to post the script Vince, I'll have a look to see why the ^M has to be there for it to work - it didn't ought to need it.
Vince Arends
Frequent Advisor

Re: ^M won't print or display, except in vi command.

In a long, round-about way, I got them to print. I telneted into my UNIX box from Windows, did vi on the file, copied the window and pasted it into WordPad, then printed. Thanks for your help. Vince
Vince Arends
Frequent Advisor

Re: ^M won't print or display, except in vi command.

In a long, round-about way, I got them to print. I telneted into my UNIX box from Windows, did vi on the file, copied the window and pasted it into WordPad, then printed. Thanks for your help. Vince
Lasse Knudsen
Esteemed Contributor

Re: ^M won't print or display, except in vi command.

OK if you are really that eager to print the ^M to the printer - try something like this:

cat | tr "015" "!" | sed -e 's/!/^M/' | lp -d

Only sideeffect is that in this example all occurences of "!" in the file is converted to "!" but you can change "!" with whatever character you want.
In a world without fences - who needs Gates ?
Bill Hassell
Honored Contributor
Solution

Re: ^M won't print or display, except in vi command.

Acyually, there is a really easy way to accomplish the printing of control characters imbedded in an ASCII file:

cat -v

This works on any kind of file including binary. In fact, it is the preferred way to list the contents of an unknown file type. The reason is that many of the binary character sequences can reprogram the terminal (or terminal emulator)and require some sort of reset to put the terminal back to normal.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: ^M won't print or display, except in vi command.

Vince:

First, I agree with John's comments above. A printer doesn't "print" it as a visible character; rather its a device control character only. The fact that vi shows it simply says its contained in the data stream. Windows & DOS want lines (records) delimited with the sequence CRLF (carriage-return/linefeed). UNIX wants just a newline character.

When you FTP in ASCII from a Windows/NT platform to UNIX (or conversely), the translation from CRLF to NL is done for you. When you do a BINARY FTP, no translation occurs, and you can end up with files like you saw in vi.

As for printers and terminals, the behavior, or I should say interpretation, of the CR and LF characters often depends upon dip-switch (hardware) or firmware settings. Sometimes the configuation is setup to generate a LF whenever a CR is sensed. An application that fails to take into account this configuration, and sends a line of data terminated with a CRLF will cause output to be double-spaced, for instance.

As John pointed out, the CR causes horizontal motion (to the left); the LF causes vertical motion (downwards). Neither on a terminal nor on a printer are the characters expressed visibly.

I hope this helps.

...JRF...
CHRIS_ANORUO
Honored Contributor

Re: ^M won't print or display, except in vi command.

Hi Vince,
Those are control characters from text/word to binary or ascii. Just do "dos2ux badfile > goodfile". You can't print them, it only appears when displayed.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Vince Arends
Frequent Advisor

Re: ^M won't print or display, except in vi command.

Thanks to all for your input. Good explanations of why they are there. Bill's answer worked!
I did a cat -v filename | lp -d and all occurrences of the ^M printed!
CHRIS_ANORUO
Honored Contributor

Re: ^M won't print or display, except in vi command.

Hi Vince,
Those are control characters from text/word to binary or ascii. Just do "dos2ux badfile > goodfile". You can't print them, it only appears when displayed.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.