------------------------------------------------------------------------ tr ------------------------------------------------------------------------ tr allows you to substitute (character to character), delete character(s), squeeze out repeated characters. Complementary selection is also possible. sed has almost all the capability of tr. however, tr has smaller learning curve and can handle control characters (especially \n) more gracefully than sed. [gsed, not sed, can also handle \n). tr copies standard input to standard output. It does not accept files like most other programs. Inputs have to be directed in. -c complement the set of values in string1 -d delete characters in string1 from input -s Squeeze multiple occurrences of the characters listed in the last operand (either string1 or string2) in the input into a single instance of the character. This occurs after all deletion and translation is completed. Now we proceed with examples, with increasing complexity. %%%%%%%%%%%%%%%%%%% I. Remap characters %%%%%%%%%%%%%%%%%%% Say, our goal is to replace "{..}" by "(..)". $ tr '{}' '()' "(", "}" -> ")" #mapping can accept range #last char of smaller string is repeated to match length of longer string $ tr "A-Z" "a-z" outfile #convert case $ tr "[:upper:]" "[:lower:]" outfile #classes can be used #usual character sets # [:digit:] # [:alpha:] [a-zA-Z] # [:alnum:] [a-zA-Z0-9] # [:blank:] space, tab # [:space:] \t,\nl,\cr,\v, \f and " " # [:cntrl:] octal 000 through 037 and 117 DEL) # [:punct:] punctuation (long list) # [:print:] printable chars (includes space but not tab) %%%%%%%%%%%%%%%%%%%%%%% II. Deleting characters %%%%%%%%%%%%%%%%%%%%%%% $ echo "abc123" | tr -d "a" #delete a specific character bc123 $ echo "abc123" | tr -d "a1" #delete chars in list bc23 $ tr -d '\r' outfile #can delete named char (DOS -> UNIX) $ tr -d \015 outfile #chars can be specified by octal value (\015 is \r) $ echo "abc123" | tr -d [:digit:] #chars in classes can be deleted abc $ echo "abc123" | tr -cd "[:digit:]" #delete all non-digits 123 #but missing \n $ tr -cd "[:print:]" < infile #remove all non-print characters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% III. Squeeze out runs of strings of same char %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Squeeze out successive characters to only one $ cho "Hello Kitty From Tokyo" | tr -s " " Hello Kitty From Tokyo $ tr -cs "[:alpha:]" "\n" < infile #convert file to a list of words -------------------------------------------------------------------------------- Useful Example -------------------------------------------------------------------------------- # Very useful: number header line entries # you have a row of headers with the entries separated by "," # you wish to assign them column numbers # e.g. header line: Index, Object Name, RA, DEC, z echo Index, Object Name, RA, Dec, z | tr ',' '\n' | nl 1 Index 2 Object Name 3 RA 4 Dec 5 z -------------------------------------------------------------------------------- PAINFUL CAVEAT (work in progress) -------------------------------------------------------------------------------- LANG settings