Negation in "tr"

Is there an option for negation in the "tr" command?

For eg:

echo hi | tr "h" "i"

will print "ii".

But if I want to covert all characters that are not "h" to "j", how do I do that?

Is there something like "!" in tr? Or any switch?

Thanks,
Prasanna

$ echo hi | sed "s/[^h]/j/g"
hj
$ echo hi | tr -c 'h\n' 'i'
hi

Thanks.

But I would like to output "hh"

by doing something like echo hi | tr (convert all non-h characters to h).

Is there a way to do this?

$ echo hi | tr -c 'h\n' 'h'
hh

What does that \n do?

And is the -c used for complement/negation?

  1. \n is just a character (linefeed) that you probably do not want converted to the character "h"
  2. yes

---------- Post updated at 00:59 ---------- Previous update was at 00:57 ----------

since you are converting any character that is not h to h, but h is h anyway, this would be equivalent:

$ echo hi there | tr -c '\n' 'h'
hhhhhhhh

And, one more question.

Suppose I have a list of values :

5
6
3
-1
-3
-7

in sorted order with all the positive and negative numbers grouped together.

Can I pipe this to a tr command and replace all the numbers not having the "-" sign by one single line, such as the \n character?

Thanks,
Prasanna

I do not understand your question. Can you give an example of your desired output?

$ echo hi | tr -c 'h\n' 'h'
hh

Actually this is outputting only "hi", if you can check.

I am getting hh

Actually I want to count the number of positive and negative numbers in the list eventually.

So I thought I could remove the numbers that don't have the minus sign in front of them using "tr" , and then count the remaining numbers.

Is there any other way to do it? To count the number of positive and negative numbers without removing? It is a question that I saw somwewhere, it asked to do without using scripts, grep, sed or awk.

---------- Post updated at 07:20 PM ---------- Previous update was at 07:15 PM ----------

I tried in a different shell now. It outputs "hh".

Can you explain exactly how that works? I am not able to understand what is replaced by what in tr -c.

Thanks,
Prasanna

---------- Post updated at 07:23 PM ---------- Previous update was at 07:20 PM ----------

Thanks, I understood how it works.

I do not see how you can do that using tr. But I believe I answered that question here, did I not?

Yes, I forgot to mention there. Without using any loops. Anyway, thanks a lot.

$ echo hi|ruby -e 'p gets.chomp!.tr("^h","j")'
"hj"
$ ruby -00 -ne 'n,p=$_.split.partition {|x| x.to_i<0};END{print "positive: #{p.size}, negative: #{n.size}\n" }' file
positive: 3, negative: 3

Is there an option in sort or uniq that could sort and print only the negative numbers, omitting the positive ones, and vice versa?

see other thread

Only certain versions of "tr" will work with the above syntax.
Most of them will just put a single "h" character in the first position of the translation table and nowhere else.

This is the correct syntax to expand the string of "h" characters to fill every slot in the transalation table except the linefeed character.

echo "hi there" | tr -c '\n' '[h*]'
hhhhhhhh
2 Likes