Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stripping carriage returns from a file

Status
Not open for further replies.

dendenners

Programmer
Jul 17, 2001
110
IE
Hi there.
I ftp files from a windows app to unix to be processed on that platform. The file processor cannot handle the ^M's that windows inserts at the end of each line. I have a script on its own that strips the <CR>'s. Here it is:
#!/bin/ksh

sed &quot;s/
//g&quot; $1

I have a util.sh shell script in which I put my utility functions. However, when I put this simple looking command into the script, it doesn't work! Does anyone have a script function that strips <CR>s from files which doesn't have to be in a .sh file on its own? Thanks.
 
Use the command sed 's/^M//g'

To get the ^M in the script use the Ctrl-V Ctrl-M key sequence. Pressing Ctrl-V tells the shell to expect a control character next.

Greg.
 
Hey there. I put the ^M in as you said. My function looks like this.
stripcr()
{
file_name=$1;
temp_strip_file=&quot;/tmp/temp_strip_file.tmp&quot;
sed 's/^M//g' > $temp_strip_file
if [ $? -ne 0 ]
then
OLOGSTR=&quot;Error creating temp <CR>-less strip file $temp_strip_file&quot;
ologerror
return 1
fi
etc....

However the problem seems to be that even if I have the ^M is as ^V^M, when the shell parses the script it interprets the ^M as being a carriage return and the script hangs in the middle of the sed, waiting for more input. Is there a way around this? Thanks.

 
Can you turn on the ascii option in ftp to have the carriage returns stripped off while ftp is tranferring the file? Otherwise, try a \ in front of the ^M. CaKiwi
 
Thanks for the input.
Unfortunately the script is still hanging on the sed command, waiting for further input after the ^M. here's how it looks now:
temp_strip_file=&quot;/tmp/temp_strip_file.tmp&quot;
sed 's/\^M//g' > $temp_strip_file

(obviously with the ^M in as ^V^M)
I can't strip the <CR>s before this stage because the windows program I am using is creating the files on the unix box itself using samba. Thanks again
 
Oh, now I see the problem. You ers not giving sed an input file. You need
[tt]
sed 's/^M//g' $1 > $temp_strip_file
[/tt]
Hope we've got it this time. CaKiwi
 
Try the following one-liner:
[tt]
perl -i -p -e 's/\r//g' file
[/tt]
If you want to do this recursively, use:
Code:
find . -type f -print | xargs perl -i -p -e 's/\r//g'
To explain:
[ul]
[li]The -i option means change in place
[li]The -p option means read in the file, and print out each line, after any processing specified with -e
[li]The -e 's/\r//g' option means perform this substitution for each line before printing. \r is the escape sequence for a carriage return.
[/ul]
Using find and xargs simply performs this operation on each file recursively. Cheers, Neil :)
 
If you have it on your unix platform dos2unix <filename> <newfilename> will strip out the carriage returns. HTH.
 
Hi,

another solution using tr

tr -d '\r' < inputfile > outputfile Jean Pierre.
 
I had the same problem and I solved it with this.
tr -d &quot;\015&quot; < file > newfile

015 is octal for ^M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top