Unix shell programming (5)
The filter TR is used to convert characters from standard input. Format:
Tr from-chars to-chars
From-chars and to-chars are one or more characters. For example:
[Root @ localhost programs] # Cat intro
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
The design of the UNIX system was to create
Environment that promoted efficient Program
Developments.
[Root @ localhost programs] # tr e x <intro
Thx unix opxrating extends XM was pionxxrxd by kxn
Thompson and dxnnis ritchix at bxll laboratorixs
In THX latx 1960 s. onx of THX primary goals in
THX dxsign of thx unix runtime XM was to crxatx
Xnvironmxnt that promotxd xfficixnt Program
Dxvxlopmxcnt.
-S option
Used to compress repeated characters in to-chars. For example:
[Root @ localhost programs] # Cat lotsapaces
This is an example of
File that contains a lot
Of blank spaces
[Root @ localhost programs] # tr-S' <lotsapaces
This is an example of
File that contains a lot
Of blank spaces
-D option
Used to delete characters in the input stream. The format is:
Tr-D from-chars
Delete all spaces
[Root @ localhost programs] # tr-d' <lotsapaces
Thisisanexampleofa
Filethatcontainsalot
Ofblankspaces
You can also use the SED command:
[Root @ localhost programs] # SED's // G' lotsapaces
Thisisanexampleofa
Filethatcontainsalot
Ofblankspaces
Tr command comprehensive instance:
------------------------------------
Tr 'X' converts all larger X to lowercase x
Tr' () ''{} 'replaces all parentheses with braces
Tr '[A-Z] ''[N-ZA-M]' converts A-M letters to N-Z and converts N-Z to A-M
Tr-D'/14' Delete All paging characters
Tr-d' [0-9] 'Delete All numbers
------------------------------------
The grep command can search for a specific string mode from one or more files. The format is:
Grep pattern files
For example, find Unix-containing rows from the file intro:
[Root @ localhost programs] # grep UNIX intro
The UNIX operating system was pioneered by Ken
The design of the UNIX system was to create
-V option displays excluded rows
For example, display all rows in the intro file that do not contain Unix:
[Root @ localhost programs] # grep-V 'unix 'intro
Thompson and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
Environment that promoted efficient Program
Developments.
For example, to display all files in the current directory that contain Unix or Unix lines:
[Root @ localhost programs] # grep '[UU] nix '*
Intro: the UNIX operating system was pioneered by Ken
Intro: the design of the UNIX system was to create
-L option only provides a list of files containing the given mode, without matching lines in the file.
[Root @ localhost programs] # grep-L '[UU] nix '*
Intro
-N: add the relative row number of the row in the file before each row in the specified mode in the option file.
[Root @ localhost programs] # grep-N 'unix '*
Intro: 1: the UNIX operating system was pioneered by Ken
Intro: 4: The design of the UNIX system was to create
Sort command:
[Root @ localhost programs] # Cat names
Tony
Emanuel
Lucy
Ralph
Fred
[Root @ localhost programs] # Sort names
Emanuel
Fred
Lucy
Ralph
Tony
-U option to remove duplicate rows in output results
-R option reverse sorting order
[Root @ localhost programs] # Sort-r names
Tony
Ralph
Lucy
Fred
Emanuel
Sort output redirection: available-O options, also available> symbol
[Root @ localhost programs] # Sort-r names> sorted_name1
[Root @ localhost programs] # Sort names-O sorted_name2
-N options are sorted by the first column
For example:
[Root @ localhost programs] # Cat data2
5 27
2 12
3 33
23 2
-5 11
15 6
14-9
[Root @ localhost programs] # Sort-N data2
-5 11
2 12
3 33
5 27
14-9
15 6
23 2
[Root @ localhost programs] # uniq names
Tony
Emanuel
Lucy
Ralph
Fred
Tony
[Root @ localhost programs] # Sort names | uniq
Emanuel
Fred
Lucy
Ralph
Tony