[PLUG] Translating ^M to \n

Rodney W. Grimes freebsd at gndrsh.dnsmgr.net
Tue Aug 13 00:41:09 UTC 2019


> > On Mon, 12 Aug 2019, Paul Heinlein
> > 
> > > Also, you can use "od -c" to verify the C-style character name tr will 
> > > recognize.
> > >
> > > od -c inffile | less
> > 
> > Tried this; less showed nothing until the end of the file.
> 
> NOTHING?  I think your missing something there, you should of
> seen \r's at the end of each line.  If infact there are no \r
> in the file then this file is NOT in windows/msdos format.
> 
> 
> > Without piping output to less the last character in the file was \r.
> 
> And the character before that was?
> 
> > 
> > Running 'cat <infile> | tr "\r" "\n" > <outfile> left outfile with the same
> > ^M as infile. Strange M$ stuff I guess,
> 
> *sigh*  you probably do not want to change the CR to a LF, you want to
> remove the LF

Error here, you want to remove the CR

> > If the files are essentially single lines and I use sed with the /g option
> > it should replace all \r with \n, but it doesn't.
> 
> sed is a line oriented tool, it is doing nasty things your not
> thinking about, it is already breaking the line at what it
> thinks are line boundaries, as are vi, emacs, etc...
> 
> tr is probably the best tool for the job.
> 
> > Whew! Thought I had it but that's not the case.
> 
> I am starting to think you file may not actually BE windows/dos
> line formatted, but something od, your output from od -c should
> have lots of \r \n sequences in it.

I think od -c does NOT output the \n.. no, looked closely at a hd
of the file.  First byte of the file is 0xa, A LF ok, so thats odd
.  After that the lines do appear to be seperated by 0x0d, which is CR
all by itself, these are NOT CR-LF windows format files, the are
CR seperated files from who knows what?

My guess.. this was a browser downloaded .csv file?  So line
endings have been mangled by the web.

> CAT infile | tr -d "\r" >outfile
> 
> Removes the CR that windows puts in files that is not used
> by unix by translate them to null.  That -d is IMPORTANT!

This file is not cr-lf, so this is invalid to do.

You might fix it by simply deleting the first byte...
then doing the cr -> lf remap

dd if=oldfile bs=1 iseek=1 | tr '\r' '\n' >newfile

Note this is gona be really slow due to single byte
I/O used by the DD command to take the first byte off.


-- 
Rod Grimes                                                 rgrimes at freebsd.org



More information about the PLUG mailing list