
LINUXLINUXUSER USER SchlagwortCommand sollte Line: hier tr & stehen dos2unix Using tr and dos2unix THE TRANSLATOR The tr tool is a real wizard. This simple command lets you replace strings in text files. Whether you are replac- ing letters or just removing whitespace, you will be amazed at tr‘s versatility. BY HEIKE JURZIK he tr command replaces charac- test.txt will replace any occurrences of ments as arrays of lower and capital ters in text files. The command “1” in the file 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 more 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 $ echo Petronella | tr 'e' 'a'U just replaced with “a.” You might prefer which sends all the numbers in a text to Patronalla to use sed 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.LINUX- 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 unix2dos 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 cat 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:] ''<test.txt allows you to specify both an input file Good Combinations and a new output file. The manpage has tr really shines when you use it in com- In this case, tr will replace contiguous details on other useful tips and tricks. bination with other shell commands. For blanks or tabs with simple blanks. The -k flag keeps the original timestamp, example, imagine you discover a large and -o writes the changes directly to the number of files with blanks in their file- Migrating between Worlds original file. unix2dos does the same names on your disk, and you want to If you often have to exchange files thing, but in the other direction: replace the blanks with underscores. A between Windows and Linux systems, for loop, the mv command, and tr will you will note that strange characters can unix2dos -n linuxtext wintext help you change the underscores to occur at ends of lines. For example, if blanks: you try to open an ASCII created in Win- On some systems, these utilities are sym- dows with the Vim editor, you will bolic links to the fromdos and todos pro- $ for i in *; do mv -v "$i" U notice a weird looking bunch of ^M grams, respectively. These programs `echo $i | tr ' ' '_'`; done characters. The reason for this is quite have a slightly different syntax and dif- 'file with blank' -> U simple: the two systems use different ferent parameters. You should use the -b 'file_with_blank' symbols for the end-of-line character. option to create a backup of the original 'file with blank 2' -> U Whereas Windows uses \r\n to denote a file, even if you are not creating a new 'file_with_blank_2' line break, Linux leaves off the \r and file. And you might like to note the -a simply keeps the \n. flag, which removes all carriage returns If we translate the preceding command tr can help you exchange ASCII files when stipulated with fromdos (and not into plain English, the command would between these two systems. Calling only those preceded by a line feed). If be: for all the files in the current direc- you specify -a with the todos command, tory do the following: move the files tr -d '\r' < wintext > linuxtext it will convert any line feeds into CR-LF visibly for the user to files whose names pairs. The default behavior in this case are the results of the tr replacement will remove the extra \r at the end of would be to convert only those line operations. ■ each line and convert the text file. The -d flag tells tr to remove the unwanted GLOSSARY Heike Jurzik studied German, Computer character. The < operator then tells the Line break: The syntax for line breaks Science and English command to parse wintext, and > sends on computers was based on the way at the University of the “clean” results to an output file typewriters work. There is a control Cologne, Germany. called linuxtext. character for the line feed, and another She discovered for the carriage return. Different operat- Linux in 1996 and has been fasci- Alternative Converters ing systems have different approaches nated with the scope of the Linux The dos2unix and unix2dos tools are to handling line breaks. Whereas Linux command line ever since. In her also useful if you need to convert back uses a simple line feed (\n = “new line”), THE AUTHOR leisure time you might find Heike DOS/Windows adds a carriage return (\r hanging out at Irish folk sessions or = “return”). visiting Ireland. WWW.LINUX- MAGAZINE.COM ISSUE 53 APRIL 2005 89.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages2 Page
-
File Size-