文本命令
文章目录
文本排序:sort [OPTION]… [FILE]…
[root@localhost tmp]# cat > a <<hzw
> 13
> 15
> 8
> 5
> 3
> 1
> 4
> 6
> 5
> 8
> 7
> 6
> hzw
[root@localhost tmp]# cat a
13
15
8
5
3
1
4
6
5
8
7
6
sort -n:以数值大小进行排序
[root@localhost tmp]# sort -n a
1
3
4
5
5
6
6
7
8
8
13
15
[root@localhost tmp]#
sort -r:逆序排序
[root@localhost tmp]# sort -r a
8
8
7
6
6
5
5
4
3
15
13
1
[root@localhost tmp]#
sort -nr:以数值大小逆序排序
[root@localhost tmp]# sort -nr a
15
13
8
8
7
6
6
5
5
4
3
1
sort -t:字段分隔符 sort -k:以哪个字段为关键字进行排序
[root@localhost tmp]# cat a
1 g
5 h
6 r
8 o
8 f
4 a
5 a
[root@localhost tmp]# sort -t' ' -k2 a
4 a
5 a
8 f
1 g
5 h
8 o
6 r
sort -u:去重,排序后相同的行只显示一次
[root@localhost tmp]# sort -t' ' -k2 -u a
4 a
8 f
1 g
5 h
8 o
6 r
sort -f:排序时忽略字符大小写
[root@localhost tmp]# sort -t' ' -k2 -f a
1 A
5 B
8 b
9 c
16 t
99 u
13 y
文本去重:uniq [OPTION]… [INPUT [OUTPUT]]
uniq -c:显示文件中行重复的次数
[root@localhost tmp]# sort -n a
1
3
4
5
5
6
6
7
8
8
13
15
[root@localhost tmp]# sort -n a|uniq -c
1 1
1 3
1 4
2 5
2 6
1 7
2 8
1 13
1 15
uniq -d:只显示重复的行
[root@localhost tmp]# sort -n a|uniq -d
5
6
8
uniq -u:只显示重复的行
[root@localhost tmp]# sort -n a|uniq -u
1
3
4
7
13
15
基础命令cut OPTION… [FILE]…
cut -d:指定字段分隔符,默认是空格
cut -f: 指定要显示的字段
cut -f 1,3:显示1和3字段
[root@localhost tmp]# cp /etc/passwd .
[root@localhost tmp]# ls
a ks-script-Wut_Pw runtime1.tar.gz runtime3.tar.xz world
hello passwd runtime2.tar.bz2 runtime.tar yum.log
[root@localhost tmp]# head 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
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost tmp]# cut -d: -f1,3 passwd
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
dbus:81
polkitd:999
avahi:70
avahi-autoipd:170
postfix:89
sshd:74
cut -f 1-3:显示1到3字段
[root@localhost tmp]# cut -d: -f1-3 passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
games:x:12
ftp:x:14
nobody:x:99
dbus:x:81
polkitd:x:999
avahi:x:70
avahi-autoipd:x:170
postfix:x:89
sshd:x:74
高级命令awk
awk -F: ‘{print $1,$3}’ passwd
[root@localhost tmp]# awk -F: '{print $1,$3}' passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
dbus 81
polkitd 999
avahi 70
avahi-autoipd 170
postfix 89
sshd 74