previous contents up next

Unix for Advanced Users

6. Manipulating Files

6.7. Miscellaneous Commands

6.7.1. Sort

The sort command does just that. To sort a file and print the results to standard output you would type:

sort inputfile

To specify an output filename, you could use the -o option, which would look like this:

sort -o sortedfile inputfile

To sort in reverse order use the -r flag. To check to see if a file is sorted, use the -c flag. Consult the man page for additional options.

6.7.2. Spell

Like sort, spell's function is pretty transparent. The spell command uses a wordlist of correctly spelled words (often /usr/dict/words) to spell check the file you specify. The program will output each word that it cannot find in its dictionary. If you would like to check the spelling of a single word, you can use the construction:

echo "wordtobechecked" | spell

If the word you entered is returned at the command line then spell thinks it is misspelled.

6.7.3. uniq

The uniq command's goal is to report or filter out any adjacent repeated lines in a file. Because uniq only looks for adjacent lines that repeat, it is most effectively used in conjunction with the sort command. Here are some examples of uniq in action.

example% cat uniq.test
This is a test.
This is a test.
TEST.
Computer.
TEST.
TEST.

uniq with no arguments:

example% uniq uniq.test
This is a test.
TEST.
Computer.
TEST.

The -d flag will print those entries that had adjacent lines repeated.

example% uniq -d uniq.test
This is a test.
TEST.

Finally we can see that the -c option counts the occurrences of each line:

example% uniq -c uniq.test 2 This is a test.
1 TEST.
1 Computer.
2 TEST.

6.7.4. tr

The tr command is short for translate characters. It accepts standard input and changes specified characters into other characters. For example:

echo a | tr a z

sends the character a into the tr program which changes the letter a into the letter z.

Using the -d flag, you can delete the characters that you specify. An example might look like this:

example%echo "look lidke thisz" | tr -d dz
look like this

A common use of tr is to convert lowercase characters to uppercase. This can be done in many ways. Here are three of them:

tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
tr a-z A-Z
tr '[:lower:]' '[:upper:]'

previous contents up next