Find the answer to your Linux question:
Results 1 to 9 of 9
I've read the man page and I'm not clear what it means for the characters in SET1 to be "complemented".I did some experiments which gave me some idea but also ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575

    Does anyone know what tr -c is supposed to do ?


    I've read the man page and I'm not clear what it means for the
    characters in SET1 to be "complemented".I did some experiments
    which gave me some idea but also confused me.
    My input ( $ is the prompt ):
    Code:
    $ tr -c 12345 a
    5555
    tttt
    Output:
    Code:
    5555aaaaaa$
    Does anyone know what the algorithm is supposed to be ?

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    It's complementing the sense of the match - ie translating if the input characters are not in SET1.

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Can you expand on that ? What kind of translation does it do ?

  4. #4
    Just Joined!
    Join Date
    May 2005
    Posts
    18

    tr is translating?

    I don't think it translates at all: it would ask in what langage
    you're writing and in what langage you want the translation
    to be if it did, wouldn't it? Plus the word to be translated,
    that would be 3 compulsory parameters. It only wants 2
    strings.

    I tried

    tr "help" "french" and it just hanged.

    Mariane

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575

  6. #6
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    No.

    -c means translate every character that is NOT in string1 to string2

    For example, suppose you wanted to make every non-alphanumeric character visible in a file, make them show as a # character:

    Code:
     tr -c '&#91;A-Z&#93;&#91;a-z&#93;&#91;0-9&#93;' '&#91;#*&#93;'  < myfile
    All the spaces, etc., all turn into # characters.

    Suppose you wanted to remove all characters that were not alphanumeric:
    Code:
     tr -cd  "&#91;A-Z&#93;&#91;a-z&#93;&#91;0-9&#93;"  < myfile

  7. #7
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Is the '[#*]' part necessary ? On my machine typing tr -c '[A-Z][a-z][0-9]' # seems
    to produce the same results as tr -c '[A-Z][a-z][0-9]' '[#*]'
    Also what happens if the second string has more than one characters ?
    On my machine only the last one seems to be used. (I'm always talking about the -c option.)

  8. #8
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    tr works on a per character basis. Not on two characters - then you want sed.

    Yes the # would also work for GNU tr.

  9. #9
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    I was thinking that if I type tr -c '[A-Z][a-z][0-9]' #@ for example
    then the 1st non-alphanumeric character would be replaced by
    # , the 2nd by @ , the 3rd by # again and so on.

    But anyway , thanks for the info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •