[comp.unix.questions] tr

cathy@ncelvax.UUCP (Cathy Benney) (11/16/89)

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. 

jik@athena.mit.edu (Jonathan I. Kamens) (11/17/89)

  No, the 'tr' command is not capable of changing a single character
in the input to multiple characters of output.

  You could probably do it really easily in perl, though :-)

  Just another person who is amused by all the perl hackers but hasn't
gotten around to learning perl himself,

Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8495			      Home: 617-782-0710

matthew@sunpix.UUCP ( Sun Visualization Products) (11/18/89)

In article <15949@bloom-beacon.MIT.EDU> jik@athena.mit.edu (Jonathan I. Kamens) writes:
}
}  No, the 'tr' command is not capable of changing a single character
}in the input to multiple characters of output.
}
}  You could probably do it really easily in perl, though :-)
}
}  Just another person who is amused by all the perl hackers but hasn't
}gotten around to learning perl himself,
}
}Jonathan Kamens			              USnail:
}MIT Project Athena				11 Ashford Terrace
}jik@Athena.MIT.EDU				Allston, MA  02134
}Office: 617-253-8495			      Home: 617-782-0710


True, but this particular problems can be handled by AWK also.

awk '{printf ("%s\r\n", $0)}' $*


[Note: code written on the fly, and untested - but should work]




-- 
Matthew Lee Stier                            |
Sun Microsystems ---  RTP, NC  27709-3447    |     "Wisconsin   Escapee"
uucp:  sun!mstier or mcnc!rti!sunpix!matthew |
phone: (919) 469-8300 fax: (919) 460-8355    |

merlyn@iwarp.intel.com (Randal Schwartz) (11/20/89)

In article <579@ncelvax.UUCP>, cathy@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@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/

jik@athena.mit.edu (Jonathan I. Kamens) (11/21/89)

In article <1004@greens.UUCP> matthew@greens.UUCP (Matthew Stier - Sun 
Visualization Products) writes:
>In article <15949@bloom-beacon.MIT.EDU> jik@athena.mit.edu (Jonathan I.
>Kamens) writes:
>}
>}  No, the 'tr' command is not capable of changing a single character
>}in the input to multiple characters of output.
>
>True, but this particular problems can be handled by AWK also.
>
>awk '{printf ("%s\r\n", $0)}' $*

  This will work, yes.  However, the original poster asked if tr can
substitute multiple characters in the output for one character in the
input, and than gave the LF -> CR + LF case as an *example* of where
that would be necessary.

  Awk will work for that one case, but it doesn't address the general
problem.  The perl script already posted (by Randal Schwartz, I
think?) is the best solution I've yet seen to the general problem.

  Oh, well, off to learn perl.... :-)

Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8495			      Home: 617-782-0710