tr

Randal Schwartz merlyn at iwarp.intel.com
Mon Nov 20 11:44:41 AEST 1989


In article <579 at ncelvax.UUCP>, cathy at ncelvax (Cathy Benney) writes:
| Hello.  I have been experimenting with "tr", the translate characters command.  I am curious to know if tr can substitute from a single character in string 1 
| to multiple characters in string 2.  For example, could "tr" substitute
| '\012' (the ascii new line) for '\015\012' (which would be ^M^J).  I have been 
| trying this, using a command like: 
|     tr '\012' '\015\012' < file.1 > file.2 
| but without any luck.  In the command above, tr substitutes ascii 012 for 
| ascii 015.  What I would like is for tr to substitute every 012 with an
| 012 015.  I am a novice in using tr (could you guess?), so my approach may be 
| off, but I would appreciate any suggestions you may have. Thank you. 

Well, first, stock out-of-the-box 'tr' cannot do it.  Tr knows how to
delete chars, and replace chars, but not add chars.  If you don't have
Perl (shame on you!), use sed, as in:

% sed 's/$/^M/' <file.1 >file.2

where ^M represents a *real* control-M, which you may have to futz
around with the terminal escape chars to get it into the string.  For
example, under BSD unicies (UNIX plural :-), I can get a ^M by
preceding it with a control-V.

Another (nicer) solution (you knew it was coming...) is Perl, as in:

% perl -pe 's/\012/\015\012/g;'

which is typed just as you see it.... no magic chars.  (Perl does all
the magic. :-)

Just another Perl hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn at iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/



More information about the Comp.unix.questions mailing list