Removing ^M Character from all the files in a folder.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing ^M Character from all the files in a folder.
# 1  
Old 01-21-2012
Removing ^M Character from all the files in a folder.

Hi Everyone,

I am new and need your help.

Can anyone please help me in removing the ^M character from all the file in a folder recursively.

Please provide me the code.
Response will be Really appreciated.

Thanks in Advance
# 2  
Old 01-21-2012
There are hundreds of replies to your question on this forum, do a search (top right)
# 3  
Old 01-21-2012
It sounds like you copied the files from a Windows system to a Unix/Linux system using a network protocol, like Samba, or NFS.
Lines in a Windows text file end with a carriage return and a line feed. Unix files just end with a line feed. The ^M that you see is the carriage return.
You can remove these carriage returns in several ways.
If you have the program "dos2unix"
Code:
dos2unix <inputfile >outputfile

if not:
Code:
tr -d "\r" <inputfile >outputfile

You can also copy the entire folder from the unix system back to a Windows system using ftp in binary mode, and then copy the files back again using ascii mode.
Code:
c:\work>ftp unix
>cd somewhere
>binary
>mget *
ascii
cd somewhere1
mput *
quit

# 4  
Old 01-21-2012
I am able to remove the ^M characters using sed s/^[0-9]$//g. what I am doing is removing the character ^M and moving it into a temp file then replacing with the original file.

but I am not able to read the filename from a folder and not able to pass the filename as a argument. Can you please provide me the code
# 5  
Old 01-23-2012
That will delete everything that's not a [0-9] character, which seems a bit drastic when all you want to delete is the carriage return.

If you don't have dos2unix, you should really use tr for this.

Code:
find /dir -type f | while read LINE
do
        tr -d '\r' <"$FILE" >/tmp/$$
        cat /tmp/$$ > "$FILE"
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

How to replace the first and last character which is pipe symbol in all files within a folder?

with in my files i have the data like this, starting with a pipe and ending the line with a pipe. all i want is to replace the first and last pipe , remove those trying to use following sed command, but it is only showing on the screen the entire data of the file as if it removed, but when i... (4 Replies)
Discussion started by: cplusplus1
4 Replies

3. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

4. Shell Programming and Scripting

Replace character in files of entire folder? sed? or what?

Hello, I do have several files in one folder each file contains measurement data. for each file I would like to replace the character "," by "." ? How can I do this and how can I do this for each file at once? E.G. data_1.dat, data_x.dat (original version) data_1out.dat, data_x_out.dat... (10 Replies)
Discussion started by: rollinator
10 Replies

5. Shell Programming and Scripting

removing last character

Hi, I have a file that has data something like below: A B C D ..... ...... .....and so on I am trying to bring it in one line with comma delimited something like below : A,B,C,D I tried the something below in the code section: cat File.txt | tr '\n' ',' (1 Reply)
Discussion started by: rkumar28
1 Replies

6. UNIX for Dummies Questions & Answers

Need help removing last character of every line if certain character

I need help removing the last character of every line if it is a certain character. For example I need to get rid of a % character if it is in the last position. Input: aaa% %bbb ccc d%dd% Output should be: aaa %bbb ccc d%dd I tried this but it gets rid of all of the % characters.... (5 Replies)
Discussion started by: raptor25
5 Replies

7. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

8. UNIX for Dummies Questions & Answers

removing the extension from all filenames in a folder

Hi there, I'm pretty new to UNIX and have tried trawling through this forum to find an answer to what I want to try to do, which I'm sure is very simple but I don't know how to do it. What I have a a folder that contains multiple files that I have copied from Windows and I want to remove the... (5 Replies)
Discussion started by: johnmcclintock
5 Replies

9. Shell Programming and Scripting

Removing a folder with no name

I have a folder with no name on unix.(someone created it) How do i remove this? anyone came across this kind of a problem please respond. thanks ram (2 Replies)
Discussion started by: ramky79
2 Replies

10. UNIX for Dummies Questions & Answers

Removing the ^M character in VI

Hello, I am attempting to remove all the ^M characters in a file in VI. The command I am using is :1,$s/^V^M//g but it doesn't work, saying 'substitute pattern match failed'. Any ideas why? Jules (2 Replies)
Discussion started by: julesinbath
2 Replies
Login or Register to Ask a Question