Thursday, 22 September 2016

Removing unwanted spaces using tr cmd in linux

     Everyone will like to get a pearl in an ocean but it's not so easy.Same way grepping a exact string from the selected content is a tricky task. I spent much time to get the exact command in internet ,so i am sharing the information here that it might helpful for all of you.


Example:

   Suppose I would like to get the exact domain from the system information using below command.While i use awk or cut command it will provide the result but it will also includes the extra spaces along the result to us.It might not helpful to us while passing such result into scripts like perl/shell etc.

      So trimming the space here is much more important to us.To achieve this we can follow final command which given here.

Commands:

1.Grepping domain name from your system:

# facter domain - xxx.yyyyyy.com

2.Need to get the xxx value from the above domain name:

# facter domain | cut -d "." -f 1 - xxx ( partial success ) this result will provide unwanted space along with this

3.Finally By using below command we can remove unwanted spaces:

# facter domain | cut -d "." -f 1 | tr -d '[:space:]' -- xxx

Brief Explanation of above command:


What is Facter:

     Facter is small program that gathers system information such as hardware details, network settings, virtualisation type and kernel/OS information. Facter was created for Puppet to gather system information, but is also available as a standalone command.

Cut:

Cut will perform cut operation,d - used as a delimiter, f - field

tr:

translate or delete characters, d - delete, [:space:] - all horizontal or vertical spaces.

No comments:

Post a Comment