Tr filter file command

  
 

You can think of tr as an (extremely) simplified variant of sed: it can replace another character with one character, or it can completely remove some characters. You can also use it to remove duplicate characters. This is what all tr ​​can do.

So why use tr instead of sed? Of course it is to make things simple. For example, if we want to replace all the letters that appear with the letter "z"; you can use tr az , this command is no doubt simpler than sed -es/a/z/g, especially in it When used in a script, the quotation marks are very confusing. In addition, when using tr, you can avoid writing annoying regular expressions.

Using tr is simple: use the symbolic notation given in the previous paragraph to replace all other characters that appear with one character. When you need to replace multiple characters, use a notation like this: tr abc xyz , which means to replace all the letters that appear with the letter “x”; a” replace all the letters with the letters “y”” ;b”, replace all letters "c” with the letters “z”. The number of characters listed in these two groups does not have to be equal.

You can also specify a range of characters. For example, tr a-z A-Z will replace all lowercase letters with corresponding uppercase letters (for example, it will convert "no smoking" into "NO SMOKING". This special technique is handy when you want to emphasize a part of the text being edited in the vi editor. Just press the Escape key, then press the :, then type 2,4!tr 'a-z' 'A-Z' and press the Return key. Now, the letters from line 2 to line 4 are converted to uppercase letters.

In addition, when someone sends you a text file created on a Mac OS or DOS/Windows
machine, you will find tr useful. If you do not save the file to use UNIX line breaks to indicate the end of line format, you will need to convert such files to native UNIX format, otherwise some command utilities will not process them properly. The end of the Mac OS ends with a carriage return character, and many text processing tools treat such a file as a single line. To correct this problem, you can use the following tips:

Mac -> UNIX: tr '\ ' '\ ' < macfile > unixfile UNIX -> Mac: tr '\ ' '\\ r' < unixfile > macfile Microsoft DOS/Windows convention, where each line of text ends with a carriage return character followed by a newline character. To correct this problem, you can use the following command:

DOS -> UNIX: tr -d '\ ' < dosfile > unixfile UNIX -> DOS: In this case, you need to use awk Because tr cannot insert two characters to replace one character. The awk command to be used is awk '{ print $0"\ " }' < unixfile > dosfile In addition, when you need to do some simple finishing work on the text file (such as using tr -d '\\t' to remove the system When you use tr -s ' ' to remove extra spaces, or use tr -d '\ ' to combine separate lines into a single line, you will need to use tr. Again, you can use all of these commands in vi; just remember: add the line range and exclamation point (!) you want to process before the tr command, such as 1, $!tr -d '\\t' (dollar sign) Shown in the last line).

Copyright © Windows knowledge All Rights Reserved