[comp.unix.questions] Convert newlines to something else.

tchrist@convex.COM (Tom Christiansen) (02/08/91)

From the keyboard of tvz@phoenix.Princeton.EDU (Timothy Van Zandt):
: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.

Dealing with \n's in sed is not as straightforward as you might wish.
Maybe someone will post a sed solution.

I would use the tr program if you want single char translations, like
perhaps 

	% tr '\012' '/' <infile >outfile

If you want more complex changes, I would use perl, perhaps like this:

	% perl -pe 's/\n/foobar /' <infile >outfile

--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI window 
manager, which argues with you for 10 weeks before resizing your window." 
### And now for the question of the month:  How do you spell relief?   Answer:
U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U

jpr@jpradley.jpr.com (Jean-Pierre Radley) (02/10/91)

In article <6011@idunno.Princeton.EDU> tvz@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.

The problem is that you go crazy trying to type the sed commands from the
a shell prompt. You need to go into 'vi' and use the ^V facility to get a true
^M into a script.

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

 Jean-Pierre Radley   NYC Public Unix   jpr@jpradley.jpr.com   CIS: 72160,1341

tchrist@convex.COM (Tom Christiansen) (02/10/91)

From the keyboard of jpr@jpradley.UUCP (Jean-Pierre Radley):
:In article <6011@idunno.Princeton.EDU> tvz@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)