Help with removing characters like ^M


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with removing characters like ^M
# 1  
Old 02-09-2007
Help with removing characters like ^M

I have few files in unix which are in dos format. While I am copying these files, ^M, ^@, etc characters are being generated.

I tried dos2unix command in Linux and it doesn't work.
I tried sed to remove these characters but they won't go.
I came to about this 'tr' command and tried to use it but when I try to use it, still it removes M elsewhere in the file but not ^M.

Please advise in how to get rid of these caret characters.

Thanks
Chiru
# 2  
Old 02-09-2007
For sure you removed uppercase M characters at the beginning of the line.

The reason why you failted to remove ^M is because you type it as 2 characters.

In the file however it is a single character.

You can remiove them e.g. in "vi".

Once inside "vi" you can create ^M as a single character by typing first CTRL-V and next CTRL-M.

Make sure you are in command mode (press ESC if necessary) and next you type:

:%s/^M//g

Make sure the ^M is a single character created as mentioned above.
# 3  
Old 02-09-2007
Too weird

This is just kind of weird. I was going to ask about this very thing when I came to the board, and the question is up on top.

I was wondering about using sed to do the trick. I am running Linux with GNU sed version 4.1.5. Anyway, I had a file which was peppered with ^M characters. I tried this:

Code:
sed -i 's/^M/\n/g' some_prog

But it did not seem to do the trick. What did I do wrong?

edit::

Ah - I just tried doing <CTRL-V> <CTRL-M> and it worked. So thanks sb008..

So my next question is about the <CTRL-V> <CTRL-M> sequence. Why is <CTRL-V> necessary? What is happening when it is used?

Last edited by kermit; 02-09-2007 at 10:19 PM..
# 4  
Old 02-10-2007
Just for a second thought you could as well accomplish the same task with sed as well. You dont have to replace ^M with the new line character(\n) just try removing the ^M character.

sed 's/^M//g' filename
# 5  
Old 02-10-2007
As sb008 said

Ctrlv and ctrlM procedure is usefule to remove "^M" ...sometimes "sed" won't work.. I faced this problem earlier
# 6  
Old 02-10-2007
>Just for a second thought you could as well accomplish the same task with sed >as well. You dont have to replace ^M with the new line character(\n) just try >removing the ^M character.

>sed 's/^M//g' filename

Indeed - that is what I actually wanted rather than inserting all the newlines.
# 7  
Old 02-10-2007
I've had to use this command-line often. It will strip carriage returns (^M) and convert null bytes (^@) into spaces.

cat dos.file | tr -d '\r' | tr '\0' ' ' > unix.file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing all characters up to initial '{'

Hi there, Im having a bit of difficulty with this one and I suspect its because of the character I want to match against maybe causing me a problem, but i wanted to remove everything up to (but not including) the first instance of '{' in a string so for example the string that I want to... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

2. Shell Programming and Scripting

Removing last and first characters in a file

bash-3.00$ cat temp.txt ./a/REA01/ces1/apps/ces_ces1_init3_aa.ear/ces.war/WEB-INF/classes/reds/common/environment.properties ./a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi/HostIntegration.properties... (9 Replies)
Discussion started by: bhas85
9 Replies

3. UNIX for Dummies Questions & Answers

Removing trailing characters

I have been given a shell script that I need to amend. To do the following extract the filename from the flag file by removing the .flag extension. # Local variables # Find if the flag files exists MASK=coda_mil2*.flag # Are there any files? bookmark="40" fileFound=0 ls -1... (3 Replies)
Discussion started by: andymay
3 Replies

4. UNIX for Dummies Questions & Answers

removing non printable characters

Hi, in a file, i have records as below: 123|62|absnb|267629 123|267|28728|uiuip 123|567|26761|2676 i want to remove the non printable characters after the end of each record. I guess there are certain charcters but not visible. i don't know what character that is exactly. I used... (2 Replies)
Discussion started by: pandeesh
2 Replies

5. Shell Programming and Scripting

Removing special characters

Dear Friends, I want to remove text between two patters. Problem is, it has random special characters like \ / | * ` ~ ! $ etc. These random special characters has no fixed length. But these special characters are appearing between a fixed pattern e.g. DM&^%#|#!\/?CT Expected output... (14 Replies)
Discussion started by: anushree.a
14 Replies

6. UNIX for Dummies Questions & Answers

Removing ^M characters

hi I have a perl script conv.pl. when i execute this file and direct i to log file I see lots of ^M characters in the log file. There is no ^M in conv.pl file. Log file is generated only after conv.pl is executed. Please help as how to get rid of these. This conv.pl is going to get schduled... (5 Replies)
Discussion started by: infyanurag
5 Replies

7. UNIX for Advanced & Expert Users

Removing ^M characters

hi I have a perl script conv.pl. when i execute this file and direct i to log file I see lots of ^M characters in the log file. There is no ^M in conv.pl file. Log file is generated only after conv.pl is executed. Please help as how to get rid of these. This conv.pl is going to get schduled... (0 Replies)
Discussion started by: infyanurag
0 Replies

8. UNIX for Dummies Questions & Answers

removing characters

Hi all, Any help on how to do the following? :eek: I have an infile as follows: _thisishowyouwritehelloworld _thisisalsohowyouwritehelloworld2 I want to delete the characters from "_" to "how" and be left with: youwritehelloworld youwritehelloworld2 I am able to do delete from a... (2 Replies)
Discussion started by: dr_sabz
2 Replies

9. HP-UX

Removing ^D and ^H characters

Hi, I have a very huge file and it contains some unprintable characters like ^H and ^D. If I try to remove using cat test1.ser| tr -d '\136 110'>newfile1 it is only removing ^and all spaces in the file. How can I remove these characters (^D ^H) and keep my spaces as it is? Thanks &... (1 Reply)
Discussion started by: arsheshadri
1 Replies

10. Shell Programming and Scripting

Removing certain characters in a file

Hi I have a file that has semicolons in it (;) is there a way to just remove these in the file. Example name: Joe Smith; group: Group1; name: Mary White; group: Group2; (2 Replies)
Discussion started by: bombcan
2 Replies
Login or Register to Ask a Question