[Reprint] linux sort, uniq, cut, wc, tr, xargs command Detailed

linux sort, uniq, cut, wc, tr, xargs command Detailed


<- Contents ->
. 1) sort
2) the uniq
. 3) Cut
. 4) WC
. 5) TR
. 6) xargs

[] sort
sort File command in the file specified in the sorting line, and writes it to standard output. If the File parameter specifies more than one file, then the sort command will connect these documents, and as a file sort.

Syntax sort

copy the code
[root @ www ~] # sort [-fbMnrtuk] [file or stdin]
options and parameters:
-f: ignoring differences of case, for example, considered the same as A with a coding;
-b: Ignore the frontmost space character part;
-M: the name of the month to be sorted, such as JAN, DEC, etc. sorting method;
-n: use "pure digital" sort (the default is to sort of character patterns);
-r: anti to sort;
-u: the uniq is, the same data, the row for only occur;
-t: separator, separated by default [Tab] key;
-k: in that interval (field) means to sort
copy Code

of / etc / passwd account sorting
[the root WWW @ ~] CAT # / etc / passwd | Sort
ADM: X:. 3:. 4: ADM: / var / ADM: / sbin / nologin
Apache: X: 48: 48: the Apache: / var / WWW: / sbin / nologin
bin: X:. 1:. 1: bin: / bin: / sbin / nologin
daemon: X: 2: 2: daemon: / sbin: / sbin / nologin
Sort default data to the first sort, and the default is to sort strings, so start ascending order by the letter a.



/ etc / passwd contents are: to separate, I would like to sort the third column, how

[the WWW root @ ~] # CAT / etc / passwd | the Sort -t ':' -k 3
root: the X-: 0 : 0: the root: / the root: / bin / the bash
UUCP: X: 10: 14: UUCP: / var / spool / UUCP: / sbin / nologin
operator: X:. 11: 0: operator: / the root: / sbin / nologin
bin: the X-: 1: 1: bin: / bin: / sbin / nologin
games: the X-: 12: 100: games: / usr / games: / sbin / nologin
default is to sort the strings, if you want to use digital Sort:

CAT / etc / the passwd | Sort -t ':' -k with 3N
the root: X: 0: 0: the root: / the root: / bin / the bash
daemon: X:. 1:. 1: daemon: / usr / sbin: / bin / SH
bin: X: 2: 2: bin: / bin: / bin / SH
default ascending order, if reverse order, as follows

CAT / etc / the passwd | Sort -t ':' -k 3NR
the nobody: X: 65534: 65534: the nobody: / NONEXISTENT: / bin / SH
NTP: X: 106: 113 :: / Home / NTP: / bin / to false
messagebus: X: 105: 109 :: / var / RUN / dbus: / bin / to false
the sshd: X: 104: 65534 :: / var / RUN / the sshd: / usr / sbin / nologin


If to / etc / passwd, in the first two characters of the sixth field to the fourth characters in a forward direction, then reverse the order based on the first field.

CAT / etc / the passwd | Sort -t ':' 6.2, 6.4 -k -k lR
Sync: X:. 4: 65534: Sync: / bin: / bin / Sync
Proxy: X: 13 is: 13 is: Proxy: / bin: / bin / SH
bin: the X-: 2: 2: bin: / bin: / bin / SH
SYS: the X-: 3: 3: SYS: / dev: / bin / SH


view the / etc / passwd how many shell: on / etc / passwd seventh ordered domain, then go weight:

CAT / etc / passwd | Sort -t ':' -k. 7 -u
the root: X: 0: 0: the root: / the root: / bin / the bash
the syslog : X: 101: 102 :: / Home / the syslog: / bin / to false
daemon: X:. 1:. 1: daemon: / usr / sbin: / bin / SH
Sync: X:. 4: 65534: Sync: / bin: / bin / Sync
the sshd: X: 104: 65534 :: / var / RUN / the sshd: / usr / sbin / nologin


[] uniq
uniq command may remove the sorted file. duplicate rows, so uniq sort often used in combination. In other words, in order to make uniq work, all duplicate rows must be contiguous.

uniq Syntax

[root @ www ~] # uniq [-icu]
options and parameters:
-i: Ignore different case characters;
-C: counting
-u: Displays unique row


contents are as follows testfile

copy the code
CAT testfile
Hello
world
Friend
the Hello
world
the Hello
copy the code


delete an unsorted file, you will find no rows are deleted

copy the code
#uniq testfile
the Hello
world
Friend
the Hello
world
the Hello
copy the code


sorting files, the default is to re-

#cat words | sort | uniq
Friend
the Hello
world


after sorting delete duplicate lines, while the first position in the output line of the line number of repetitions

#sort testfile | uniq -c
1 Friend
3 the Hello
2 world


show only duplicate line and the first line displays the line number of repetitions

# testfile Sort | -dc the uniq
. 3 Hello
2 World


display not only duplicate rows

Sort testfile | -u the uniq
Friend


[cut]
cut command extracts the text from a text file or column of the text stream.

cut Syntax

[root @ www ~] # cut -d ' separator character' -f fields <== separator character for specific
[root @ www ~] # cut -c character interval <== for rows of information
options and parameters:
-d: followed by the separator character. Used in conjunction with -f;
-f: separating character based -d piece of information will be dividing the number of stages, taken by means of paragraphs -f;
-C: In units of characters (characters) fixed character extraction section;


the PATH variable as

[the root WWW @ ~] $ echo the PATH #
/ bin: / usr / bin: / sbin: / usr / sbin: / usr / local / bin: / usr / X11R6 / bin: / usr / games
# 1 | 2 | 3 | 4 | 5 | 6 | 7


the PATH variable out, I'll find the fifth path.

$ PATH #echo | Cut -d ':' 5 -f
/ usr / local / bin


to your PATH variable out, I'll find the third and fifth paths.

$ PATH #echo | Cut -d ':' -f 3, 5
/ sbin: / usr / local / bin


to your PATH variable out, I want to find a path to the last third.

$ PATH echo | Cut -d ':' of 3--f
/ sbin: / usr / sbin: / usr / local / bin: / usr / X11R6 / bin: / usr / games


will remove the PATH variable, I want to find out first to a third path.

$ PATH #echo | Cut -d ':' -f 1-3
/ bin: / usr / bin: / sbin:



the PATH variable out, I want to find out first to third, and fifth paths.

the PATH $ echo | Cut -d ':' -f 1-3, 5
/ bin: / usr / bin: / sbin: / usr / local / bin

md5sum / etc / Shadow | Cut -b -5
before five characters display

practical examples: displays / etc / passwd user and the shell

#cat / etc / passwd | Cut -d ':' -f l, 7
the root: / bin / the bash
daemon: / bin / SH
bin: / bin / SH


[wc]
statistical document how many words there are, how many rows, the number of characters.

wc syntax

[root @ www ~] # wc [-lwm]
options and parameters:
the -l: lists only lines;
-w: how many words (English words) only lists;
-m: how many characters;


default statistics wc / etc / the passwd

#wc / etc / the passwd
40 45 1719 / etc / the passwd
40 is the number of lines, number of words is 45, the number of bytes 1719



wc command is relatively simple to use, for each parameter used as follows:

copy the code
#wc -l / etc / passwd # count the number of rows, when the number of records, it is commonly
40 / etc / passwd # the system has 40 accounts

#wc -w / etc / passwd # count the number of times the word appears
45 / etc / passwd

#wc - the number of bytes m / etc / passwd # statistics file
1719

[tr]
tr is mainly used to delete the file control characters or character conversions.
Tr with the most common options command format is:
tr -s -c -d [ "string1_to_translate_from"] [ "string2_to_translate_to"] <the INPUT-File
here:
complement -c string in a character set replace the character set, character set is required ASCII.
-d delete all input character string 1.
-s remove all repeated sequences of characters, retaining only the first; soon repeated string compressed into a string.
input-file is to convert the file name. Although you can use other input format, but this format is the most commonly used.
2, the range of characters
when the character string or the contents of a specified string 2, use only a single character or a string or list range.
[az] az character string in the composition.
[AZ] AZ character string within the composition.
[0-9] numeric string.
\ octal a three-digit octal number, corresponding to the valid ASCII characters.
[O * n] represents O character is repeated specified number of times n. Thus [O * 2] OO of matching string.
tr Different expressions specific control characters
shorthand meaning octal embodiment
\ a Ctrl-G tones \ 007
\ B the Ctrl-H backspace \ 010
\ F the Ctrl-L Traveling feed \ 014
\ n-the Ctrl-J newline \ 012
\ r Ctrl-Enter M \ 015
\ t Ctrl-the Tab key to the I \ 011
\ v the X-Ctrl-\ 030
example:
the "abc" appear in the document file replace "xyz"

# CAT file | TR "abc" "xyz"> new_file

Note: Here, all the file appears in the letter "a" are replaced with "x" letter, "b" is the replacement of letters "y" letter, "c" replaced letter "z" letter. Instead of the string "abc" is replaced with the string "xyz".

2, using the tr command "unified" case sensitive
(lowercase -> uppercase)
# CAT File | tr [az] [AZ]> new_file
(uppercase -> lowercase)
# CAT File | tr [AZ] [az]> new_file

3, the digital file is replaced with 0-9 AJ

# CAT file | TR [0-9] [AJ]> new_file

4, "of Snail" delete file file appears in the character

# cat file | tr -d "Snail "> new_file

Note: here, all appear in the file documents 'S', 'n', 'a', 'i', 'l' character will be deleted! The "Snail" is not tightly delete appears string.

5, wrap delete files file appears in '\ n', tab '\ t' character

# cat file | tr -d "\ n \ t">

new_file invisible characters have to use escape characters to represent, and that is unified.

6, delete "continuous with the" repeated letters, retaining only the first

# CAT File | TR -s [A-zA-the Z-]> new_file

7, delete blank lines

# cat file | tr -s "\ n"> new_file

8. Remove Windows files "cause" of the '^ M' character

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

Note: This parameter -s followed by two "\ r" and "\ n" , replacing the latter with the former

9, with a space character \ replace tabs 040 \ 011

# CAT File | TR -s "\ 011" "\ 040"> new_file

10, the path variable colon ":", replace newline "\ the n-"

# $ echo the PATH | TR -s ":" "\ the n-"

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11372652.html