Search code examples
linuxuppercaselowercasetr

Command tr upper and lower extract


If I want to convert the text part after the - character like could I do with the tr command?

Using this command applies the function on all the text

cat file.txt | tr [: upper:] [: lower:]

01103I-GG102
01103I-GG102
01103I-GG102
01103I-GG102
01103I-HH'2
01103I-HH'2
01103I-HH'2
01103I-HH'2
01103I-HH'2
01103I-HH'2
01103I-HH'2
01103I-HH'2
01103I-HH'3
01103I-HH'12
01103I-HH'12
01103I-HH'12
01103I-HH'12
01103I-HH'12
01103I-HH'12
01103I-HH'12
01103I-HH'22
01103I-HH'22
01103I-HH'22
01103I-HH'42
01103I-HH'42
01103I-HH'42
01103I-HH'42
01103I-HH'42
01103I-HH'42
01103I-HH'43
01103I-GG62
01103I-GG62
01103I-GG62
01103I-GG62
01103I-GG62
01103I-GG62
01103I-GG62
01103I-GG62
01103I-GG63
01103I-GG63
01103I-GG63
01103I-GG63
01103I-GG63
01103I-GG63
01103I-GG63
01103I-GG52

You can help me?

Thank you


Solution

  • If I were you I would go for a sed solution. The following sed command does transform your input file as requested in your post (to lowercase after the -)

    $ sed 's/\(.*\)-\(.*\)/\1-\L\2/g' file
    01103I-gg102
    01103I-gg102
    01103I-gg102
    01103I-gg102
    01103I-hh'2
    01103I-hh'2
    01103I-hh'2
    01103I-hh'2
    01103I-hh'2
    01103I-hh'2
    01103I-hh'2
    01103I-hh'2
    01103I-hh'3
    01103I-hh'12
    01103I-hh'12
    01103I-hh'12
    01103I-hh'12
    01103I-hh'12
    01103I-hh'12
    01103I-hh'12
    01103I-hh'22
    01103I-hh'22
    01103I-hh'22
    01103I-hh'42
    01103I-hh'42
    01103I-hh'42
    01103I-hh'42
    01103I-hh'42
    01103I-hh'42
    01103I-hh'43
    01103I-gg62
    01103I-gg62
    01103I-gg62
    01103I-gg62
    01103I-gg62
    01103I-gg62
    01103I-gg62
    01103I-gg62
    01103I-gg63
    01103I-gg63
    01103I-gg63
    01103I-gg63
    01103I-gg63
    01103I-gg63
    01103I-gg63
    01103I-gg52
    

    Explanations:

    • s/\(.*\)-\(.*\)/\1-\L\2/g you use sed in replacing mode and capture the 2 groups surrounding the -, then you use back reference as replacement and transform to lower case the 2nd group (that contains the pattern after the -) using \L option.

    tr usage:

    If you are sure that you have only H and G in the second part of each line and that they do not appear in the fist part then you could imagine something like tr 'HG...' 'hg...' but this the maximum this little tr command can do.