首页 > 代码库 > linux常用命令-文本处理cut,sort,uniq,wc,tr
linux常用命令-文本处理cut,sort,uniq,wc,tr
cut:截取文本特定字段
NAME
cut - remove sections from each line of files
-d, --delimiter=DELIM(指定字段分隔符,默认是空格)
use DELIM instead of TAB for field delimiter
-f, --fields=LIST(指定要显示的字段)
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
[rhel@localhost ~]$ 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
[rhel@localhost ~]$ cut -d : -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
sort:文本排序,以ascii
NAME
sort - sort lines of text files
SYNOPSIS
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
[rhel@localhost ~]$ cat sort.txt
3
5
1
4
9
[rhel@localhost ~]$ sort sort.txt
1
3
4
5
9
[rhel@localhost ~]$
-n, --numeric-sort(数值排序)
compare according to string numerical value
-r, --reverse(反向排序)
reverse the result of comparisons
[rhel@localhost ~]$ sort -r sort.txt
9
5
4
3
1
[rhel@localhost ~]$
-t, --field-separator=SEP(字段分隔符)
use SEP instead of non-blank to blank transition
-k, --key=POS1[,POS2](关键字进行排序)
start a key at POS1 (origin 1), end it at POS2 (default end of line)
[rhel@localhost ~]$ 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(删除重复项)
with -c, check for strict ordering; without -c, output only the first of an equal run
[rhel@localhost ~]$ cat sort.txt
3
5
1
4
43
9
4
11
[rhel@localhost ~]$ sort -u sort.txt
1
11
3
4
43
5
9
uniq:显示或者忽略重复行
NAME
uniq - report or omit repeated lines
-d, --repeated(只显示重复行)
only print duplicate lines
[rhel@localhost ~]$ uniq sort.txt
3
5
1
4
43
9
4
11
[rhel@localhost ~]$ uniq -d sort.txt
3
1
[rhel@localhost ~]$
-D, --all-repeated[=delimit-method](显示所有重复)
print all duplicate lines delimit-method={none(default),prepend,separate} Delimiting is
done with blank lines.
[rhel@localhost ~]$ uniq -D sort.txt
3
3
1
1
[rhel@localhost ~]$
-c, --count(显示文件行重复的次数)
prefix lines by the number of occurrences
[rhel@localhost ~]$ uniq -c sort.txt
2 3
1 5
2 1
1 4
1 43
1 9
1 4
1 11
[rhel@localhost ~]$
wc:显示文件中行数/单词数/字节数
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
[rhel@localhost ~]$ wc /etc/fstab
15 78 805 /etc/fstab
[rhel@localhost ~]$ man wc
[rhel@localhost ~]$ wc -l /etc/fstab
15 /etc/fstab
-w, --words
print the word counts
[rhel@localhost ~]$ wc /etc/fstab
15 78 805 /etc/fstab
[rhel@localhost ~]$ wc -w /etc/fstab
78 /etc/fstab
[rhel@localhost ~]$
-c, --bytes
print the byte counts
[rhel@localhost ~]$ wc /etc/fstab
15 78 805 /etc/fstab
[rhel@localhost ~]$ wc -c /etc/fstab
805 /etc/fstab
-L, --max-line-length(最长行字符数)
print the length of the longest line
[rhel@localhost ~]$ wc -L /etc/fstab
93 /etc/fstab
tr:转换或者删除字符
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
[rhel@localhost ~]$ tr ‘ab‘ ‘AB‘
abc
ABc
able
ABle
again
AgAin
^C
[rhel@localhost ~]$
[rhel@localhost ~]$ 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 characters in SET1, do not translate
[rhel@localhost ~]$ tr -d ‘ab‘
alb^H^H^[[3~^[[3~
‘
‘
able
le
a
enable
enle
linux常用命令-文本处理cut,sort,uniq,wc,tr