tr command file filtering, splitting and merging

The tr command can replace, compress, and delete characters from standard input. It can turn one set of characters into another, and is often used to write beautiful one-line commands, and is very powerful.

grammar

tr (options) (parameters)

Options

-c or --complerment: replace all characters that do not belong to the first character set;
-d or --delete: delete all characters belonging to the first character set;
-s or --squeeze-repeats: represent consecutively repeated characters as a single character;
-t or --truncate-set1: First delete the characters in the first character set that are more than the second character set.

parameter

    • Character Set 1: Specifies the original character set to convert or delete. When performing a conversion operation, the target character set for conversion must be specified with the parameter "charset2". But when the delete operation is performed, the parameter "character set 2" is not required;
    • Character Set 2: Specifies the target character set to convert to.

example

Convert input characters from uppercase to lowercase:

echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world

Both 'AZ' and 'az' are sets, and sets can be made by themselves, for example: 'ABD-}', 'bB.,', 'a-de-h', 'a-c0-9' belong to sets , '\n', '\t', and other ASCII characters can be used in the collection.

Use tr to remove characters:

echo "hello 123 world 456" | tr -d '0-9'
hello  world 

Convert tabs to spaces:

cat text | tr '\t' ' '

The complement of the character set, which removes all characters from the input text that are not in the complement:

echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n'
 1  2  3  4

In this example, the numbers 0~9, spaces and newlines \n are included in the complement, so they are not deleted, and all other characters are deleted.

Compressing characters with tr compresses repeated characters in the input:

echo "thissss is      a text linnnnnnne." | tr -s ' sn'
this is a text line.

Cleverly use tr to add numbers:

echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo $[ $(tr '\n' '+') 0 ]

Remove the '^M' character "caused" by Windows files:

cat file | tr -s "\r" "\n" > new_file
or
cat file | tr -d "\r" > new_file

Character classes that tr can use:

[:alnum:]: letters and numbers
[:alpha:]: alphabet
[:cntrl:]: Control (non-printing) characters
[:digit:]: digit
[:graph:]: graphic character
[:lower:]: lowercase letters
[:print:]: printable characters
[:punct:]: punctuation mark
[:space:]: whitespace character
[:upper:]: uppercase letters
[:xdigit:]: Hexadecimal characters

How to use:

tr '[:lower:]' '[:upper:]'

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851448&siteId=291194637