Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
google site



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-08-2010
Registered User
 

Join Date: Feb 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
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 06:06 PM.. Reason: code tags please!
Sponsored Links
  #2  
Old 02-08-2010
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,763
Thanks: 2
Thanked 17 Times in 16 Posts

Code:
tr -d \\015 < file

  #3  
Old 02-08-2010
Registered User
 

Join Date: Feb 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
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
Registered User
 

Join Date: Feb 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
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 scottn; 02-09-2010 at 02:59 AM.. Reason: Again, code tags please...
  #5  
Old 02-09-2010
Registered User
 

Join Date: Feb 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,763
Thanks: 2
Thanked 17 Times in 16 Posts
Try this


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

Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
removing new line from the shell variable value thegeek UNIX for Dummies Questions & Answers 5 10-07-2009 11:06 AM
best way for removing comment from shell scripts -- bash thegeek Shell Programming and Scripting 2 09-15-2009 09:36 AM
Removing the entire file contents using unix shell script. praka Shell Programming and Scripting 5 02-10-2009 02:34 AM
help removing characters for output file in shell script wingchun22 Shell Programming and Scripting 1 08-02-2008 12:55 AM
Removing line breaks from a shell variable lyonsd Shell Programming and Scripting 5 09-12-2006 01:42 PM



All times are GMT -4. The time now is 07:51 AM.