Removing ^M from last line alone of a UNIX File

Hi All,

I am copying a file from windows to UNIX. After that copying it have the ctrl+M character in the file at the line break. But the file contains the data that also have ctrl+M. I want to re move the ctrl+M at the end of the line alone.

My file structure is XML and last line doesnt contains the ^M character.

eg.

aaaaaaa^M
bbbbbbb^M
ccccccc^M
ddddddd

I tried using sed for removing the ^M, after the using the command sed 's/.$//g/' the output will be like

aaaaaaa
bbbbbbb
ccccccc

(last line is removed)

When i tried using the tr command, it is functioning the same way like dos2unix.

Please suggest a solution for this.

In all likelihood, the problem is that your last line in not a line. (I.e., it is not only missing the carriage return character ( ^M ); it is also missing the newline character.)

To verify this, please post the output of the command:

od -c file

where file is the name of your XML file.

The handling of incomplete lines varies from system to system. What operating system and shell are you using?

Thanks Don!
Exactly precise to the point. The problem is the XML File need to be untouched in the UNIX after copying as well as in the windows before copying. This actually removes the tag present in the last line. Is there any way to solve this issue.

OS is Sun Solaris 5.1 (but the behaviour is same across the other UNIX OS as well)
Shell is Korn Shell

If your files are all like this and you want to process them on UNIX Systems and you want to keep carriage return characters in your file that do not appear at the end of a line immediately before a newline character, the following should work:

#!/bin/ksh
echo >> file  # Add line terminator to partial line at end of file.
sed 's/^M$//' file > file.$$ && mv file.$$ file

(where the ^M in the sed substitute command is a literal carriage return character; not the two characters ^ and M ).

Using tr -d or dos2unix is simpler than sed if it is OK to get rid of all carriage return characters (instead of only those that appear at the end of a line).