Linux Common commands-Text processing CUT,SORT,UNIQ,WC,TR

Source: Internet
Author: User
Tags gopher

Cut: Intercepting text-specific fields

NAME
Cut-remove sections from all line of files


-D,--delimiter=delim (Specify field delimiter, default is space)

Use DELIM instead of the TAB for field delimiter


-F,--fields=list (Specifies the field to display)

Select only these fields; Also print any line that contains no delimiter character,
Unless the-s option is specified
-F 1,3
-F 1-3


[Email protected] ~]$ CAT/ETC/PASSWD
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

[Email protected] ~]$ cut-d:-f1/etc/passwd
Root
Bin
Daemon
cd/
Lp
Sync
Shutdown

Sort: Text sort, in ASCII

NAME
Sort-sort lines of text files

Synopsis
Sort [OPTION] ... [FILE] ...
Sort [OPTION] ...--files0-from=f


[email protected] ~]$ cat Sort.txt
3
5
1
4
9
[Email protected] ~]$ sort Sort.txt
1
3
4
5
9
[Email protected] ~]$


-N,--numeric-sort (numeric sort)
Compare according to string numerical value


-R,--reverse (reverse sort)
Reverse the result of comparisons

[Email protected] ~]$ sort-r sort.txt
9
5
4
3
1
[Email protected] ~]$


-T,--field-separator=sep (field delimiter)
Use SEP instead of Non-blank to blank transition

-K,--key=pos1[,pos2] (keywords to sort)

Start a key at POS1 (Origin 1), end it at POS2 (default end of line)

[Email protected] ~]$ sort-t:-k3-n/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin


-U,--unique (delete duplicates)
With-c, check for strict ordering; Without-c, output only the first of the equal run

[email protected] ~]$ cat Sort.txt
3
5
1
4
43
9
4
11
[Email protected] ~]$ sort-u sort.txt
1
11
3
4
43
5
9

Uniq: Show or ignore duplicate rows

NAME
Uniq-report or omit repeated lines


-D,--repeated (only duplicate rows are displayed)
Only print duplicate lines

[Email protected] ~]$ Uniq sort.txt
3
5
1
4
43
9
4
11
[Email protected] ~]$ uniq-d sort.txt
3
1
[Email protected] ~]$


-D,--all-repeated[=delimit-method] (show all duplicates)
Print all duplicate lines Delimit-method={none (default), prepend,separate} delimiting are
Done with blank lines.

[Email protected] ~]$ uniq-d sort.txt
3
3
1
1
[Email protected] ~]$


-C,--count (shows the number of times the file line repeats)
Prefix lines by the number of occurrences

[Email protected] ~]$ uniq-c sort.txt
2 3
1 5
2 1
1 4
1 43
1 9
1 4
1 11
[Email protected] ~]$

WC: Show the number of rows in a file/number of words/bytes

NAME
Wc-print newline, Word, and byte counts for each file

Synopsis
WC [OPTION] ... [FILE] ...
WC [OPTION] ...--files0-from=f


-L,--lines
Print the newline counts

[Email protected] ~]$ Wc/etc/fstab
805/etc/fstab
[email protected] ~]$ man WC
[Email protected] ~]$ wc-l/etc/fstab
15/etc/fstab


-W,--words
Print the word counts

[Email protected] ~]$ Wc/etc/fstab
805/etc/fstab

[Email protected] ~]$ wc-w/etc/fstab
78/etc/fstab
[Email protected] ~]$


-C,--bytes
Print the byte counts

[Email protected] ~]$ Wc/etc/fstab
805/etc/fstab

[Email protected] ~]$ wc-c/etc/fstab
805/etc/fstab


-L,--max-line-length (maximum number of characters in line)
Print the length of the longest line

[Email protected] ~]$ wc-l/etc/fstab
93/etc/fstab

TR: convert or delete characters

NAME
Tr-translate or delete characters

Synopsis
TR [OPTION] ... SET1 [SET2]


[[Email protected] ~]$ TR ' AB ' AB '
Abc
Abc
Able
ABle
Again
AgAin
^c
[Email protected] ~]$


[[Email protected] ~]$ TR ' A-Z ' A-Z ' </etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown


-D,--delete (delete character set)
Delete characters in SET1, does not translate

[[email protected] ~]$ tr-d ' AB '
alb^h^h^[[3~^[[3~


Able
Le
A


Enable
Enle




Linux Common commands-Text processing CUT,SORT,UNIQ,WC,TR

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.