Monday, April 9, 2012

Awesome use case for tr command.
Use case : Remove all whitespaces but one from the output of any of the unix command ( for e. g. free -m )

Current command output:
[root@host ~]free -m
                    total       used       free     shared    buffers     cached
Mem:         16046      15779        267          0         20      14269
-/+ buffers/cache:       1489      14557
Swap:         4000          0       4000


with tr,
[root@host ~]free -m |tr -s " "
 total used free shared buffers cached
Mem: 16046 15778 268 0 20 14269
-/+ buffers/cache: 1488 14558
Swap: 4000 0 4000

where,
-s : replace each input sequence of a repeated character that is listed  in  SET1( in our case it is space character " " ) with a single occurrence of that character.

In this way we can get output with only one space character as a delimiter.

No comments:

Post a Comment