The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

LinkBacks (?)
LinkBack to this Thread: http://www.unix.com/shell-programming-scripting/28780-trying-remove-m-characters-file.html
Posted By For Type Date
jatorrero's Bookmarks on Delicious This thread Refback 12-26-2008 07:59 AM

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove null characters from file? siegfried UNIX for Dummies Questions & Answers 1 04-02-2008 01:02 AM
remove space characters melanie_pfefer Shell Programming and Scripting 1 03-11-2008 01:45 PM
How to remove Characters before '~' Amey Joshi UNIX for Dummies Questions & Answers 4 01-07-2008 06:43 AM
remove multiple characters once norsk hedensk Shell Programming and Scripting 2 08-04-2003 03:13 PM
Remove control characters aravind_mg UNIX for Dummies Questions & Answers 5 10-02-2002 02:07 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack (1) Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 06-30-2006
Krispy Krispy is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 9
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 -d "^M" < originalfile.csv > newfile.csv
2) tr -d "\015" < originalfile.csv > newfile.csv

....but when I vi & set list the new file, I am still seeing the trailing $ character in the file.

Can anyone please tell me where I am going wrong?

Thanks in advance,
Kev
  #2 (permalink)  
Old 06-30-2006
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
Hope this helps.

I had a big issue with this while trying to get a flat file into Oracle. I tried using vi and sed, and ultimately came upon a solution using strings and tr.

Here is my shell script:

#make a backup
cp $1 $1.orig

#Strip out nulls.
tr -d ‘\0′ < $1.orig > $1.temp

#Strip out other unprintable characters
strings $1.temp > $1

#Remove temp file.
rm $1.temp

#Fix permissions. (You may not need this for your application)
chmod 744 $1
  #3 (permalink)  
Old 06-30-2006
thestevew thestevew is offline
Registered User
  
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
Have you misunderstood vi's "set list". What it does is print a "$" sign at the end of each line (and show tabs as ^I). The $ sign isn't in the file itself it is just displayed to make it easier for you to spot things like trailing spaces at the end of lines.

cheers
  #4 (permalink)  
Old 06-30-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,858
The "^M" things are from dos/windows files. dos carriage returns are "\n\r", unix "\n"

The extra character shows up as "^M".

Use dos2ux (or dos2unix depending on your flavor of unix) to convert windows text files.


Code:
dos2ux oldfile > newfile

  #5 (permalink)  
Old 06-30-2006
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360
in vi you can replace all ^M characters with command
:%s/^M//g

(Note that to type ^M in vi type Ctrl-V Enter)

I realise that this note may be offensive for vi users
  #6 (permalink)  
Old 07-03-2006
Krispy Krispy is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 9
Thanks for the swift response guys - much appreciated.

Thestevew was right - I was misunderstanding the vi 'set list' option, so thanks for putting me straight.

I eventually came up with a couple of possibilites - I could use the unix2dos command within a batch file to properly format my csv file.

I also found another thread on this forum with the following perl command...


Code:
perl -i -pe 's/$/\r/' myfile.csv

I'll probably use the perl command to avoid implementing a batch file. Plus, I don't have to output the results to a new file.

Out of interest, does anyone know of a sed command that would do the same as the above perl command?

Thanks again,
Kev
  #7 (permalink)  
Old 07-03-2006
Hitori's Avatar
Hitori Hitori is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2006
Posts: 360

Code:
sed -i 's/$/\r/' myfile.csv

I don't think that all implementations of sed has this feature. I've used GNU sed 4.1.4
Sponsored Links
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 07:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0