Linux Training - The tr Command

One of the training objectives for LPI certification, covered in our fundamentals of linux training, is familiarity with some common Linux commands. The list of commands that students are required to know is quiet long and is spread across several LPI objectives (see here).  Although most of them are command that are used everyday that will become familiar to any Linux administrator, there are some obscure ones like "fmt" and "pr". When trying to teach these commands to students studying to become Linux administrators a good example, like a picture, is worth a thousands lines of man pages :)

The tr Command Training Example

One of the commands that is easy to understand but hard to come up with decent examples for is the "tr" command. As per the man page description "Translate,  squeeze,  and/or  delete  characters from standard input, writing to standard output." Essentially it is used to replace one character with another and this is often used in examples. Given the text

"Linux Training Is Great"

The following command simply replaces all capital letters with their corresponding lower case letters:

echo "Linux Training Is Great" | tr TLIG tlig

Produces:

linux training is great

Simple enough, but students are left wondering if they will ever practically use this command, especially since there are more powerful text replacement and manipulation utilities such as sed and awk. Especially since, more often than not, you want to replace entire words. BTW the above could be more efficiently done like so:

echo  "Linux Training Is Great" | tr [:upper:] [:lower:]

This example makes use of the posix character classes. But one of the best ways to demonstrate tr is to use it in combination with other commands. This is true for most unix commands, as the unix philosophy is to chain commands that perform a single function well, together to achieve a desired result. Buts where as grep, tail or head have a use by themselves some commands, like tr, do not.

Good Training Examples

So what are good training examples for tr, that demonstrate how it will be practically used and just how convenient it is to have this command in your toolbox? The best examples is to use tr in cleaning up input files or data for manipulation with other commands later. Practically, using "tr" to replace single quotes, or other undesirable characters, in csv files that needs to be imported into a database later can be quiet handy.

echo  "Linux Train'ing I's Great" | tr -d  \'

The "-d" option deletes the match. The resultant text can then be sent through to mysql for insertion into a database.  A good example that can be used in a Linux fundamentals training class, when students may not be that familiar with databases, is to us the "squeeze" functionality of tr together with the cut command. The squeeze functionality of "tr" replaces a sequence of repeated characters with a single instance. This is particularly handy when you want to use the cut command with a file that uses spaces for its field delimiter, but has occurrences of repeated spaces littered throughout the file.

A good example of such a file is a log file. Lets take the squid access log file for example. Here is an example of a squid access log:

Jan 27 23:57:12 firewall squid[17950]: 1296165432.669      1 192.168.1.29 TCP_HIT/200 1132 GET http://static.url.co.za/some.css username1 NONE/- text/css
Jan 27 23:57:13 firewall squid[17950]: 1296165433.098     36 192.168.1.234 TCP_HIT/200 1968 GET http://another.co.za/index.php user2 NONE/- text/css

What we can see here is that spaces separate the fields but there are cases, like after field 6 and before 7, with multiple spaces between fields, this is obviously common in fixed length files. Although the cut command can quiet easily handle fixed length records by using the "-c" option which take the offset range of characters, the pain of correctly counting all the characters to determine the characters positions can put anyone off, and besides, you may have a variable length file with multiple random spaces. This is where the "squeezing" functionality of tr comes in handy. You can replace multiple spaces  with just  one with "tr -s ' '". The -s option replaces "each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character". So to get a listing of all users listed in the access log and then sort them alphabetically you can use the following command.
 

cat /var/log/squid/access.log | tr -s ' ' | cut -d ' ' -f 13 |sort |uniq

This is a practical example that we use in our Linux training that students can wrap their head around
 

 

Comments

Linux Training course

Url:http://www.onecoursesource.com/Discount

The Linux Training Programme is an “innovatory new training replica” for hopeful Linux, which combines the Training surrounded by communal perform and teaching external of community do. The new training programme was introduced by the "One Course Source" of Linux Training.

Thanks for this cool

Thanks for this cool article!!