Читать книгу Linux Bible - Christopher Negus - Страница 215

Translate or delete characters (tr)

Оглавление

The tr command is a character-based translator that can be used to replace one character or set of characters with another or to remove a character from a line of text.

The following example translates all uppercase letters to lowercase letters and displays the words mixed upper and lower case as a result:

 $ FOO="Mixed UPpEr aNd LoWeR cAsE" $ echo $FOO | tr [A-Z] [a-z] mixed upper and lower case

In the next example, the tr command is used on a list of filenames to rename any files in that list so that any tabs or spaces (as indicated by the [:blank:] option) contained in a filename are translated into underscores. Try running the following code in a test directory:

 for file in * ; do f=`echo $file | tr [:blank:] [_]` [ "$file" = "$f" ] || mv -i -- "$file" "$f" done

Linux Bible

Подняться наверх