Stripping Carriage Returns
Brian K. White
brian at aljex.com
Thu Apr 1 10:41:15 PST 2004
DataDoIt wrote:
> I've seen it posted in the past about a small sed or awk script that
> will strip out extra carriage returns from a file.
>
> I'm on Linux. Thnx.
extra carriage returns? or all carriage returns?
two different problems and one is simpler than the other:
if the problem is to simply strip all carriage returns and leave line-feeds
then this is it:
tr -d "\r" <in.txt >out.txt
if the problem is that some lines might have invalid line-endings like
blah blah blah<CR><LF><CR>
or
blah blah blah<LF><CR><CR>
etc...
and you want to regularize the file to be all CRLF, then perhaps:
tr -d "\r" <in.txt |awk '{print $0"\r"}' >out.txt
That strips all CR's then adds a single one at the end of each line before
the LF
There are also stand alone binaries you can find and install that do this
with names like dos2unix and unix2dos or d2u & u2d and probably a few other
similar. On sco, sco ships with two things xtod and dtox. the above tr & awk
commands are universal though. The same exact commands would work on any
unix out of the box.
----------
Just in case anyone notices that the above only deals with stripping or
correcting CR's and doesn't offer a way to do the same for LF's:
There is no generic way to detect "extra LF's" because the lf's just plain
are the definition of a "line". How can any program know that you don't
want, say, 5 empty lines? 5 empty lines is just 5 LF's in a row.
So if you have extra LF's then you have to write a dedicated script tailored
to one specific data format you happen to be dealing with where you just
happen to know that you don't want any blank lines at all, or no more than
one at a time etc...
Brian K. White -- brian at aljex.com -- http://www.aljex.com/bkw/
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx Linux SCO Prosper/FACTS AutoCAD #callahans Satriani
More information about the Filepro-list
mailing list