Removing ^M from last line alone of a UNIX File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing ^M from last line alone of a UNIX File
# 1  
Old 07-08-2014
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.

Code:
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

Code:
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.

Last edited by Don Cragun; 07-08-2014 at 11:59 PM.. Reason: Add CODE tags.
# 2  
Old 07-09-2014
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:
Code:
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?
# 3  
Old 07-09-2014
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
# 4  
Old 07-09-2014
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:
Code:
#!/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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing last character of a specific line from a file

Hello guys, I would need to remove the last character ")" of a specific line. This can be from any line. Your help is appreciated. Below is the line. HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)) Please help. (6 Replies)
Discussion started by: sang8g
6 Replies

2. Shell Programming and Scripting

Help in removing control M and Line feed in output file.

Hi All, In my output file i am getting control m character and also the line feeds at different places and with different combinations, the content of the file is supposed to be in a single line but if there is a line feed in between then from there onwards it's going into new line. I tried... (7 Replies)
Discussion started by: Bipin Kumar
7 Replies

3. Shell Programming and Scripting

Removing SAS multi line comments in UNIX

i have to remove the commented (/* . . . .*/) part which starts in one line and ends in other.help me with generic code because i have 1000 to 10k lines code which i have to remove. data one; set work.temp; input name age; infile filename; /* dfsdf dsfs sdfdf dsdd sdfsf sdfsf sfs... (4 Replies)
Discussion started by: saaisiva
4 Replies

4. Shell Programming and Scripting

Removing the sas comment line using UNIX

I have tried a lot, Need your help guys. SAS Program: data one ; /* Data step */ Input name $; /*Dec variables*/ I want to remove the commented part(/* Data step */) alone. I have tried using sed command but it is deleting the entire line itself. i need unix command to separate this and... (6 Replies)
Discussion started by: saaisiva
6 Replies

5. Shell Programming and Scripting

Removing same line from a file

Hello , I got a text file like that with many same line main/a/antlr/ main/a/apache2/ main/a/app-install-data-ubuntu/ main/a/apparmor/ main/a/apport/ main/a/apport/ main/a/apport/ main/a/apport/ main/a/apr-util/ main/a/apr-util/ main/a/apt/ main/a/apt/ main/a/apturl/the output... (1 Reply)
Discussion started by: davidkhan
1 Replies

6. Shell Programming and Scripting

Removing last character from each line of file

How can I remove the last character from each line of a file? This must be done without "funny" characters, as I want to transfer the code to/from Windows. Any ideas? (17 Replies)
Discussion started by: cjhancock
17 Replies

7. Shell Programming and Scripting

Removing last line from file

Hi, I searched the forums but could not find anything that was very useful. I am trying to remove the very last line from a file and display the rest of the file. Is there a way to use cut and tail together? Any other suggestions? Thanks (8 Replies)
Discussion started by: akeenabawa
8 Replies

8. Shell Programming and Scripting

Removing text from a line in a file

Hi All, I would like to know how to remove text from a line in a file. eg to The 4 sets of numbers are not static ie they change on each line in each different file so if anyone can help that would be great. Jeremy (10 Replies)
Discussion started by: outthere_3
10 Replies

9. Shell Programming and Scripting

Removing a particular line from a text file

Hi, I have a file called inp.txt the contents of the file are as follows MANI123|23|41 MANI123|96|23 I want to reove the first line of this file. How can I do it. Thanks in advance (5 Replies)
Discussion started by: sendhilmani123
5 Replies

10. Shell Programming and Scripting

Removing Carriage Return and or line feed from a file

Hello I'm trying to write a shell script which can remove a carriage return and/or line feed from a file, so the resulting file all ends up on one line. So, I begin with a file like this text in file!<CR> line two!<CR> line three!<CR> END!<CR> And I want to end up with a file... (1 Reply)
Discussion started by: tbone231
1 Replies
Login or Register to Ask a Question