Among other things, case conversion solves a problem created by some utilities that
copy MS-DOS files onto a system. They copy the files using the uppercase
convention of MS-DOS, and the file names need to be converted to lowercase to
work correctly. Assuming a directory full of files named in uppercase, the
following command will rename all the files to lowercase versions. The
command takes each file name and echoes it through a pipe using
tr to change
uppercase to lowercase. The result is used as the target of a
$ for name in * > do > mv $name `echo $name|tr [A-Z] [a-z]` > done $
tr includes the
-s switch, which squeezes repeating instances of the
output characters to one instance.
In the following example, the file test.txt contains one line with several
spaces between the words. The
tr command translates each
space into another space, but the
-s option compacts multiple spaces into a single
output space. The resulting file, test2.txt, has a single space between each word.
$ type test.txt How are you today? $ tr -s " " " " < test.txt >test2.txt $ type test2.txt How are you today?
Fancy line numbering with nl
nl utility adds line numbers to a file. Although this would seem like a
nl has a great number of options. To illustrate some of these
options, we're going to undertake the old-fashioned task of adding line
numbers to a Cobol program. I chose this example because it's a great way of illustrating many of the features of
nl. The following listing is
hello.txt, a Cobol program with missing line numbers.
$ type hello.txt IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. PROGRAM-BEGIN. DISPLAY "Hello world." PROGRAM-DONE. STOP RUN.
The first pass at this is simply to add line numbers, as in the following
listing. The output has several problems.