The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Carriage Return at end of file bd_joy Shell Programming and Scripting 14 10-20-2006 10:20 AM
Removing Carriage Return and or line feed from a file tbone231 Shell Programming and Scripting 1 02-18-2005 01:37 PM
sed removing carriage return and newline mored Shell Programming and Scripting 2 05-06-2004 09:28 AM
Removing Carriage return to create one record r1500 Shell Programming and Scripting 3 02-06-2004 12:45 PM
Removing carriage return characters from file b1saini UNIX for Dummies Questions & Answers 3 09-10-2003 06:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-19-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Issue with Removing Carriage Return (^M) in delimited file

Hi - I tried to remove ^M in a delimited file using "tr -d "\r" and "sed 's/^M//g'", but it does not work quite well. While the ^M is removed, the format of the record is still cut in half, like
a,b, c
c,d,e

The delimited file is generated using sh script by outputing a SQL query result to tab delimited file. The ^M embedded in the record is captured from user input, I believe.

Any insight is appreciated.
Reply With Quote
Forum Sponsor
  #2  
Old 03-19-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
Question Can you provide a hex or octal dump of the file?

Or at least the first few lines of data?

try something like the following which will look at the first five lines:
(use your file in place of infile)

>head -5 infile | od -An -t oC -w10

Note, in octal
<CR> carriage return = 015
<LF> line feed = 012

Last edited by joeyg; 08-27-2008 at 08:23 PM. Reason: removed an extra dash in command
Reply With Quote
  #3  
Old 03-19-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Re: Can you provide a hex or octal dump of the file?

Hi joeyg,

Thanks for your reply. Unfortunately, this is sensitive data so I can't send out a dump. I did use the od command to pull the data and checked for \r (od -c). I see that there is carriage return (\r)+ new line control (\n) embedded in the record.

When I used tr -d "\r\n" , however it remove all line breaks with just "\n". Do you know how I can remove only the string "\r\n" without touching those with only "\n"?

Thanks,
sirahc
Reply With Quote
  #4  
Old 03-20-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
Question Without a sample file, I am just thinking...

How about something like:

Code:
infile="original.dat"
outfile="revised.dat"
while read zf
  do
    printf "$zf\n" >>$outfile
done < $infile
Reply With Quote
  #5  
Old 03-20-2008
Registered User
 

Join Date: Feb 2008
Posts: 15
how abt dos2ux / dtox command ?
Reply With Quote
  #6  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Thansk for all your suggestions

Thanks for all your suggestions. The printf/dos2unix commands did work to some extend but cause format problem to other columns. I eventually used SQL to remove ^M before outputting it to the file.
Reply With Quote
  #7  
Old 03-24-2008
Registered User
 

Join Date: Oct 2006
Posts: 7
in vi:

# vi datafile
:s/^V^M//g
:wq
#

the ^V^M sequence means control-V control-M
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0