Removing ^M through shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing ^M through shell script
# 1  
Old 02-08-2010
Removing ^M through shell script

Hi all,

I am trying to write a ksh script to remove controlM characters from the input csv file.

I tried following command
Code:
sed 's/^M//g' $DIR/$FILENAME > $DIR/$Temp_FILENAME

while writing the code i had to press CTRL key then V then M (to type ^M)

Cant I write ^M without pressing ctrl+V ctrl+M?? Problem is when I migrate the code to some other server I have to bring the code to Windows and then to the next Unix server and the ^M char gets lost in windows.

Is there a way that sed command can read ^M without pressing ctrl+ V M manually??. e.g [CTRL]V M
so that i dont have to manually type in ^M in the script in production

Please help.

Thanks

Last edited by DukeNuke2; 02-08-2010 at 07:06 PM.. Reason: code tags please!
# 2  
Old 02-08-2010
Code:
tr -d \\015 < file

# 3  
Old 02-08-2010
example using sed

Here is an example that should process all of your .csv files.

Code:
for CSV_FILE in *.csv; 
  do if [ -f $CSV_FILE ]; 
      then sed -e 's/\x0d//g' $CSF_FILE > /tmp/foo.$$; 
      mv /tmp/foo.$$ $CSF_FILE; 
  fi;
done

# 4  
Old 02-08-2010
Resolved

thanks everyone

following also works fine in the script
Code:
sed 's/'"$(printf '\015')"'$//g' $DIR/$FILENAME > $DIR/$Temp_FILENAME

Thanks
again

Last edited by Scott; 02-09-2010 at 03:59 AM.. Reason: Again, code tags please...
# 5  
Old 02-09-2010
To replace the ^M 's on the screen in vi type

:%s/\r//

What that says is: for lines 1 to end, replace \r with nothing . This will replace only one instance of ^M per line. If you have two on a line for some reason simply run the command a second time.

The th^Me thus providing t^Mhe ability to change characters, ^mwords, and lines

I tried doing it in command mode with this text in my vi editor... It doesnt delete ^M, but it threw me an error

E486: pattern not found: r//

Please explain in detail.
# 6  
Old 02-09-2010
Try this

Code:
:1,$ s/[ctrl-key+v and ctrl-key+M]//g 
but actual command would be 
:1,$ s/^V^M//g

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and removing the old files and zipping the files using shell script

Hi, I am trying to removing the old files which were older than 10 days and same g zipping the files using the shell script. script was return as follows. find /jboss7_homes/JBOSS7/SKYLIV??/SKYLIV??_CRM/jboss-eap-7.0/standalone/log -mtime +10 -type f | xargs rm -f find /cer_skyliv??/log... (6 Replies)
Discussion started by: venkat918
6 Replies

2. Shell Programming and Scripting

Shell Script Help..Renaming Quoted files removing the timestamp

Hi all, i am new to this forum, unix and shell scripting. I would really appreciate if you all can help me here.. I have files coming in the below format 'filename20513'13May06:03:45 filename are characters.. like 'ABDDUT20513'13May06:03:45 i need it to be renamed as... (17 Replies)
Discussion started by: khman
17 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Removing control-m characters from shell script

Hi All, I need to remove control m character from a file. Steps which i am doing in shell script are: 1) We are comparing the header of the file to the database table header Here the file header has control-m characters. How do i remove it. Please help. Below are the steps i am using,... (12 Replies)
Discussion started by: abhi_123
12 Replies

4. Shell Programming and Scripting

Removing lines from a file being used by another process using SHELL script

Hi All, Need a small help in writing a shell script which can delete a few lines from a file which is currently being used by another process. File gets appended using tee -a command due to which its size is getting increased. Contents like : 25/09/2012 05:18 Run ID:56579677-1 My... (3 Replies)
Discussion started by: nikhil8
3 Replies

5. Shell Programming and Scripting

Removing matching text from multiple files with a shell script

Hello all, I am in need of assistance in creating a script that will remove a specified block of text from multiple .htaccess files. (roughly 1000 files) I am attempting to help with a project to clean up a linux server that has a series of unwanted url rewrites in place, as well as some... (4 Replies)
Discussion started by: boxx
4 Replies

6. Shell Programming and Scripting

Unix Shell scripting, removing hex 0d 0a

hi, I have a file with data like this : 5963491,11926750,Policy Endorsement 1 Policy Endorsement 2 Policy Endorsement 3 Policy Endorsement 4 Policy Endorsement 5 Policy Endorsement 6 Policy Endorsement 7 5963492,11926751,Product.Quote... (10 Replies)
Discussion started by: mrsindhe87
10 Replies

7. UNIX for Dummies Questions & Answers

removing new line from the shell variable value

I have a small doubt that, how to chomp the new line from the bash shell variable value. In perl, i will do chomp, but how to do it in bash -- shell programming. When i redirect the date output i have a new line in it, which i would want to remove, how to do ?! (5 Replies)
Discussion started by: thegeek
5 Replies

8. Shell Programming and Scripting

Removing the entire file contents using unix shell script.

I need to remove the entire file contents in file using the shell script. Actually the grap -v command will create one more file and it occupy the space also. I need to remove the entire file contents without creating new file using the shell scripting. Please help me. (5 Replies)
Discussion started by: praka
5 Replies

9. Shell Programming and Scripting

help removing characters for output file in shell script

hi i'm new to shell scripts and have a small problem i am running a batch converter that returns all flash .flv files in a directory and create a png image from each one the problem i have is the $1 variable , its ok on the first call but on the secound call $1.png , i have extra... (1 Reply)
Discussion started by: wingchun22
1 Replies

10. Shell Programming and Scripting

Removing line breaks from a shell variable

Here is my snippet of code... getDescription() { DESCRIPTION=$(dbaccess dncsdb - << ! 2>/dev/null|sed -e 's/hctt_description//' -e '/^$/ d'|tr -d '\r' select hct_type.hctt_description from hct_type,hct_profile where hct_type.hctt_id=hct_profile.hctt_id and... (5 Replies)
Discussion started by: lyonsd
5 Replies
Login or Register to Ask a Question