tr and xargs do not remove space characters

2.1k views Asked by At

I am trying to use this command to remove all < and > and + characters from a string. But the command tr is inserting a space character in its place. I want to also remove all spaces.

In

<ModelName>.<123456798123465>.

Out

ModelName . 123456798123465 . 

Expected Output

ModelName.123456798123465.

Command used

String | tr '<>+,' ' '

What am I missing here ?

3

There are 3 answers

0
Avinash Raj On BEST ANSWER

You could use sed instead of tr like below.

$ echo '<ModelName>.<123456798123465>.' | sed 's/[+<>]//g'
ModelName.123456798123465.

Character class [+<>] matches all the + or < or > symbols. By replacing the matched characters with an empty string will give the desired output.

0
rici On

If you want to delete specific characters, use tr -d SET. tr SET ' ' means to translate all characters in SET to space characters.

$ echo '<ModelName>.<123456798123465>.' | tr '<>+' ' '
 ModelName . 123456798123465 .
$ echo '<ModelName>.<123456798123465>.' | tr -d '<>+'
ModelName.123456798123465.
0
nu11p01n73R On

What you need is the -d option of tr command

$ echo "<ModelName>.<123456798123465>." | tr -d '<>+,'
ModelName.123456798123465.

From the man pages

 -d, --delete
              delete characters in SET1, do not translate