Visit Our UNIX and Linux User Community


How to remove ^M characters recusrsively from all subdirectories and file in directry


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove ^M characters recusrsively from all subdirectories and file in directry
# 1  
Old 07-30-2009
How to remove ^M characters recusrsively from all subdirectories and file in directry

Hi,

First apologies for starting the old issue (already discussed in this forum).

How can I remove ^M characters from a directory which contains lot of subdirectory and files (this includes jar, war, .xml, .properties etc). Noticeable is that, all files might not contain ^M characters.

Very obviously I can not open jar/war to check for the ^M characters.

Is there any way to proceed recursively check all the editable files (like .java/.properties/.xml/etc ) and remove characters on demand.

If I should write a script then how should I approach? I am not clear with previsous discussions.

Regards,
Bhaskar
# 2  
Old 07-30-2009
Take a backup of all the files before you try this, just in case.

I am not sure myself if it would not harm binary files etc. that have ^M in them somewhere, if you straight strip them off. So maybe you create a list of files that might be interessting for you to be stripped:

Code:
find . -type f -exec file {} \;| grep text$| cut -d":" -f1 > outfile

My "file" command gives output like this:
Code:
./css/buch/exercise files/07_whitespace/styles/base.css: ASCII C program text
./css/buch/exercise files/07_whitespace/ex0704.html: HTML document text
./css/buch/exercise files/07_whitespace/ex0703.html: HTML document text

.. so I used "text$" to filter out the text files. You have to check your output maybe and alter the one or other thing.

Check the outfile if you can live with the listed files being stripped.
Next strip off the ^M from the files in the list, write it to a .bak file and then mv the .bak back to the original filename and so with overwrite the original.

Code:
while read FNAME; do
     tr -d '\015' < "${FNAME}" > "${FNAME}".bak
     mv "${FNAME}".bak "${FNAME}"
done < outfile

# 3  
Old 07-30-2009
Tools Another resource for the ^M issue

There are two fairly standard programs to assist with ^M issues, and the sharing of files between pc and unix worlds. Research into dos2unix and unix2dos.
Perhaps the use of one of these (most likely dos2unix) will assist you.
# 4  
Old 08-05-2009
Many Thanks to both of you for this help.

Regards,
Bhaskar

Previous Thread | Next Thread
Test Your Knowledge in Computers #754
Difficulty: Medium
EEPROM stands for electronic erasable programmable read-only memory.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove non printing characters from file

How do I remove the printer escape sequence, the first 5 characters, that occurs on every 33rd line in a file, see hex dump of line 1. 0000 1e 00 00 00 00 0a 0a 0a 20 0a 20 20 20 20 20 20 .... 0010 20 20 20 20 20 20 20 20 20 20 0a 42 49 4c 4c 20 Thanks, (2 Replies)
Discussion started by: jgt
2 Replies

2. Shell Programming and Scripting

Remove characters from the file

i know , the below question has been repeated. can you guys guide me . I have the below input 999999 xxxxxxxxxxxxxx 123.45 2013-05-02 08:14 1 1 1 xxxx 999999 xxxxxxxxxxxxxx 123.45 2013-06-02 02:14 1 4 1 dddd i need to remove from the column 54 to 70 , as like the below output.... (9 Replies)
Discussion started by: expert
9 Replies

3. UNIX Desktop Questions & Answers

Remove new line characters from a file

I tried using below command tr -cd "" < InputFile.xml > output.txt ============= This removes all the tabs/newline/extra spaces from a file it successfully removed all the extra spaces,tabs and new line characters but then the complete file become one record. I want to retain one new line... (1 Reply)
Discussion started by: saini
1 Replies

4. Shell Programming and Scripting

Remove the characters from the file

Hi, I have one file in the following format. exa_resu_adj.4ge v.47645 PERSONAL INFORMAIONS PVT LTD 31 Dec 2009 04:36 Page 1 SALARY REPORT Account Account Name CCode Bill No Balance T Amt ----------- ------------ ------- ---------- ------------- ------------- 17490001 Mr Ram PM 10... (6 Replies)
Discussion started by: Kattoor
6 Replies

5. Shell Programming and Scripting

How to remove ^M characters from a zip file?

Hi All, As all of us know that while moving a file from Windows to Unix some unwanted ^M characters appear in the file. For my case I have release package in zip format which looks like Module_Name_Tag.zip. It contains some directory structure...like Module_Name_Tag.zip | |--trunk/... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

6. Shell Programming and Scripting

Trying to remove '^M' characters from a file.

Hi guys, Hope you are all well. This is a line of data from a csv file. I have used vi and set the 'set list' option to display the trailing $ character. "01","Grocery","01006","eat Fish & Spreads"$ I have tried the following commands, but neither of them appear to be working? 1) tr... (13 Replies)
Discussion started by: Krispy
13 Replies

7. Shell Programming and Scripting

Remove characters from file name

Here is my code. for file in *1.3.html ; do mv "$file" `echo $file | tr '.1.3' ''` ; done For some reason I am getting an error. mv: file.idlesince.1.3.html and file.idlesince.1.3.html are identical Could this be done a different way? (5 Replies)
Discussion started by: mrlayance
5 Replies

8. UNIX for Dummies Questions & Answers

How to remove null characters from file?

I'm trying to remove the null characters from a file and copy it to std output. I'm using emacs and I create the following one line bash file (followed by the error messages): sed -e 's/^@//' <ConfigItemReplicator.install.log /usr/bin/bash: -c: line 0: unexpected EOF while looking for... (1 Reply)
Discussion started by: siegfried
1 Replies

9. Shell Programming and Scripting

delete last line in all the file in a directry

Hi Friends, Following script will replace "prd123" to "tst123" for all the .csh file the directry. for i in *.csh echo '$i' do ex - ${i} <<EOF %s/prd123/tst123/g wq! EOF done Like this can I make a script which will delete the last line of all .csh file in the directry. ... (9 Replies)
Discussion started by: bikas_jena
9 Replies

10. Shell Programming and Scripting

How to Remove Ctrl M characters in files from directories and its subdirectories

Hi, How to recursively remove Ctrl M characters in files from a directory and its sub directory ? I know unix2dos command is there but to remove in bunch of files ... ? Thanks (7 Replies)
Discussion started by: skdp
7 Replies

Featured Tech Videos