Convert newlines to something else.
Tom Christiansen
tchrist at convex.COM
Mon Feb 11 01:30:18 AEST 1991
>From the keyboard of jpr at jpradley.UUCP (Jean-Pierre Radley):
:In article <6011 at idunno.Princeton.EDU> tvz at phoenix.Princeton.EDU (Timothy Van Zandt) writes:
:>How does one convert newlines to something else in a text file? I cannot
:>figure out how to do it with sed, if it is possible at all.
:This script uses sed to either add or delete CR's, depending on how it's called
:(i.e, store it as addcr, linked to delcr).
: # addcr : adds CRs
: # delcr : removes CRs
: [ $# -ne 2 ] && echo Usage\: $0 infile outfile && exit
: case $0 in
: *addcr) sed s+$+^M+ <$1 >$2;;
: *delcr) sed s-^M--g <$1 >$2;;
: esac
That doesn't work. The main problem is that carriage-returns (\r, 015)
are not the same as newlines (\n, 012), and under UNIX, it's newlines
that you have in your textfiles. You will find that replacing the ^M's
with ^J's doesn't work in sed, nor are \n's going to help you without
a lot more monkeying around.
Minor nits: those sed expressions need to be quoted -- my sh blew up on
the addcr sed, although my ksh was content with them; so much for bug
compatibility. Another one is that were this to actually work, it could
have quite easily been written as a filter, rather than a program that
requires known (and singular) input and output files. For example, the
delcr line could have been written like this (assuming that this expr
worked, which it doesn't):
*delcr) sed 's+$+^M+' $*;;
One perl version of addcr is:
perl -pe 's/\n/\n\n/'
and one for delcr is:
perl -pe 's/\n//'
Now, it's actually faster to do this for delcr:
perl -pe chop
But that might scare aware folks who want it to look more like how
sed really ought to work. :-)
--tom
--
"All things are possible, but not all expedient." (in life, UNIX, and perl)
More information about the Comp.unix.questions
mailing list