Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!convex!usenet From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.unix.questions Subject: Re: Convert newlines to something else. Message-ID: <1991Feb10.143018.14664@convex.com> Date: 10 Feb 91 14:30:18 GMT References: <6011@idunno.Princeton.EDU> <1991Feb10.030329.2094@jpradley.jpr.com> Sender: usenet@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Distribution: usa Organization: CONVEX Software Development, Richardson, TX Lines: 49 Nntp-Posting-Host: pixel.convex.com 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)