LINUXLINUXUSER USER SchlagwortCommand sollte Line: hier & stehen dos2unix

Using tr and dos2unix THE TRANSLATOR The tr tool is a real wizard. This simple lets you replace in text files. Whether you are replac- ing letters or just removing whitespace, you will be amazed tr‘s versatility. BY HEIKE JURZIK

he tr command replaces charac- .txt will replace any occurrences of ments as arrays of lower and capital ters in text files. The command “1” in the test.txt with “2” and send letters, for example: Treads the standard input and the result to standard output. Note that sends the results to standard output. But the characters within the arguments you tr 'a-z' 'A-Z' < test.txt of course, you can use the familiar oper- pass to the utility are handled as though ators to redirect both streams. In fact, tr they were in separate fields. Each char- If you prefer, you can simply reference really shines in combination with other acter in the first argument is replaced by the cases as follows: shell commands. its counterpart in the second argument. For example, tr ‘abc’ ‘xyz’ will replace tr [:lower:] [:upper:] U Simple Replacements “a” with “x”, “b” with “y”, and “c” with < test.txt The tr command expects two strings as “z.. If the second string is shorter than arguments and replaces all the occur- the first string, tr fills the gap with the Everything but… rences of the first argument in a text last character in the second string. The tr command has a few parameters with the second argument. This may For example, you can enter tr 'abc' 'z' that give you granular control. For sound complicated, but let’s look at a < test.txt without fazing tr. The utility example, you can use the -d option to simple example. The following com- simply replaces all occurrences of “a,” delete things, as in, mand replaces the “e“s in “Petronella” “b,” and “c” with “z.” However, tr fails if with “a“s: you try to replace, “ä” with “ae.” The tr -d '0-9' < test.txt fields are not the same length, and “ä” is $ Petronella | tr 'e' 'a'U just replaced with “a.” You might prefer sends all the numbers in a text to Patronalla to use for these more complex the happy hunting grounds. A combina- replacement tasks tion of this option and -c gives you an Of course you can specify the strings you even neater way of removing superflu- want to replace within a file. tr '1' '2' < Case Sensitive ous content. Let’s assume you want to tr can be extremely useful if you need to remove everything apart from space swap lower- and uppercase letters. The characters, uppercase characters, and best approach is two define both argu- lowercase letters; you can use -c to tell tr what not to delete:

tr -c -d 'A-Z a-z' < test.txt

88 ISSUE 53 APRIL 2005 WWW.- MAGAZINE.COM Command Line: tr & dos2unix LINUXUSER

In combination with -s, tr allows you to and forth between Linux and Windows. feeds that are not preceded by a carriage reduce the volume of a file – a useful To convert a Windows text file to the return. ability if you have, say, a logfile full of right format for Linux, simply enter: The two examples of dos2unix and whitespace. The -s option expects either shown previously could thus one or two arguments. For example, tr -s $ dos2unix -n wintext U look like the following: ' ' < test.txt removes all the whitespace linuxtext from a file. But if you simply need to dos2unix: converting file U wintext | fromdos -a U remove double spaces, or tabs, and wintext to file linuxtext U > linuxtext insert a simple blank instead, you can in UNIX format ... cat linuxtext | todos -a U supply tr -s with two arguments: >wintext This command uses the -n option, which tr -s [:blank:] ''

WWW.LINUX- MAGAZINE.COM ISSUE 53 APRIL 2005 89