007 字符处理命令


字符处理命令

字符处理命令:sort, uniq, cut,wc

sort命令

对字符进行排序。

格式

sort  需要排序的对象

[root@localhost ~]# cat test.txt 
s 5
d 6
e 1
h 2
j 9
s 5
h 2
h 2
p 11
o 12
[root@localhost ~]# sort test.txt 
d 6
e 1
h 2
h 2
h 2
j 9
o 12
p 11
s 5
s 5

# 注:经过对比得出,sort命令默认情况下对需要排序的对象的第一列进行排序。

参数

  • -k(n) :指定列进行排序,默认情况下是以空格进行分割

    [root@localhost ~]# sort test.txt 
    d 6
    e 1
    h 2
    h 2
    h 2
    j 9
    o 12
    p 11
    s 5
    s 5
    [root@localhost ~]# sort -k2 test.txt 
    e 1
    p 11
    o 12
    h 2
    h 2
    h 2
    s 5
    s 5
    d 6
    j 9
    
    # 注:-k参数只对第一个字符进行排序
    
  • -n : 按照数值的大小进行排序

    [root@localhost ~]# sort -k2 test.txt 
    e 1
    p 11
    o 12
    h 2
    h 2
    h 2
    s 5
    s 5
    d 6
    j 9
    [root@localhost ~]# sort -nk2 test.txt 
    e 1
    h 2
    h 2
    h 2
    s 5
    s 5
    d 6
    j 9
    p 11
    o 12
    
  • -r : 按照倒叙进行排序

    [root@localhost ~]# sort -nk2 test.txt 
    e 1
    h 2
    h 2
    h 2
    s 5
    s 5
    d 6
    j 9
    p 11
    o 12
    [root@localhost ~]# sort -rnk2 test.txt 
    o 12
    p 11
    j 9
    d 6
    s 5
    s 5
    h 2
    h 2
    h 2
    e 1
    
  • -t : 指定分隔符, 后面的排序按照分割符分割的列进行排序

    [root@localhost ~]# sort -t ":" -rnk3 test.txt 
    polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    ntp:x:38:38::/etc/ntp:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    halt:x:7:0:halt:/sbin:/sbin/halt
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    sync:x:5:0:sync:/sbin:/bin/sync
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    bin:x:1:1:bin:/bin:/sbin/nologin
    root:x:0:0:root:/root:/bin/bash
    

习题

# 复制/etc/passwd到/root/text.txt

cp /etc/passwd /root/text.txt

1、按照倒叙进行排序
sort -r /root/text.txt

2、按照:进行分割,第三列进行排序
sort -t ":" -nk3 /root/text.txt

uniq

对结果集进行去重。

格式

uniq [参数]  需要去重的对象

[root@localhost ~]# sort test.txt 
d 6
e 1
h 2
h 2
h 2
j 9
o 12
p 11
s 5
s 5
[root@localhost ~]# sort test.txt | uniq 
d 6
e 1
h 2
j 9
o 12
p 11
s 5

参数

  • -c : 显示重复的次数

    [root@localhost ~]# sort test.txt 
    d 6
    e 1
    h 2
    h 2
    h 2
    j 9
    o 12
    p 11
    s 5
    s 5
    [root@localhost ~]# sort test.txt | uniq 
    d 6
    e 1
    h 2
    j 9
    o 12
    p 11
    s 5
    [root@localhost ~]# sort test.txt | uniq -c
          1 d 6
          1 e 1
          3 h 2
          1 j 9
          1 o 12
          1 p 11
          2 s 5
    # 注: -c参数主要用来显示重复的次数
    
  • -d : 只显示重复的列

    [root@localhost ~]# sort test.txt | uniq -c
          1 d 6
          1 e 1
          3 h 2
          1 j 9
          1 o 12
          1 p 11
          2 s 5
    [root@localhost ~]# sort test.txt | uniq -c -d
          3 h 2
          2 s 5
    [root@localhost ~]# 
    
  • -u: 只显示不重复的列

    [root@localhost ~]# sort test.txt | uniq -c
          1 d 6
          1 e 1
          3 h 2
          1 j 9
          1 o 12
          1 p 11
          2 s 5
    [root@localhost ~]# sort test.txt | uniq -c -u
          1 d 6
          1 e 1
          1 j 9
          1 o 12
          1 p 11
    [root@localhost ~]# 
    

习题

# 复制/etc/passwd到/root/demo.txt

1、按照最后一列(以:进行分割)进行排序,去重

sort /root/demo.txt | uniq 

2、按照最后一列(以:进行分割)进行排序,去重,重复的列

sort /root/demo.txt | uniq -d

cut

剪切文件

格式:

cut  [参数]  [操作对象]

[root@localhost ~]# sort -t ":" -nk3  /etc/passwd | cut -d ":" -f1,7
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
adm:/sbin/nologin
lp:/sbin/nologin
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt
mail:/sbin/nologin
operator:/sbin/nologin
games:/sbin/nologin
ftp:/sbin/nologin
ntp:/sbin/nologin
tss:/sbin/nologin
sshd:/sbin/nologin
dbus:/sbin/nologin
postfix:/sbin/nologin
nobody:/sbin/nologin
abrt:/sbin/nologin
systemd-network:/sbin/nologin
polkitd:/sbin/nologin

参数

  • -d : 指定分割符

    [root@localhost ~]# sort -t ":" -nk3  /etc/passwd | cut -d ":" -f1,7
    root:/bin/bash
    bin:/sbin/nologin
    daemon:/sbin/nologin
    adm:/sbin/nologin
    lp:/sbin/nologin
    sync:/bin/sync
    shutdown:/sbin/shutdown
    
  • -f : 指定显示的列

    [root@localhost ~]# sort -t ":" -nk3  /etc/passwd | cut -d ":" -f1,7
    root:/bin/bash
    bin:/sbin/nologin
    daemon:/sbin/nologin
    adm:/sbin/nologin
    lp:/sbin/nologin
    sync:/bin/sync
    shutdown:/sbin/shutdown
    

习题

[root@qls ~]# echo "root:x:0:0:root:/root:/bin/bash" >test.txt
[root@qls ~]# cat test.txt
root:x:0:0:root:/root:/bin/bash
#取出第七列
cat test.txt | cut -d ":" -f7

#取出第一列和第七列
cat test.txt | cut -d ":" -f1,7

#取出第五列到第七列
cat test.txt | cut -d ":" -f5,7

tr

删除或替换结果集

格式

tr [参数] [操作对象]

[root@localhost ~]# cat /etc/passwd | tr "root" "ROOT"
ROOT:x:0:0:ROOT:/ROOT:/bin/bash

参数

  • -d : 删除结果集中的某个字符

    [root@localhost ~]# cat /etc/passwd | tr -d "root"
    :x:0:0::/:/bin/bash
    

WC

统计,计算数字

格式

wc  [参数]  [操作对象]
[root@localhost ~]# cat /etc/passwd | wc -l
21
[root@localhost ~]# 

参数

  • -l : 统计行数

    [root@localhost ~]# cat /etc/passwd | wc -l
    21
    [root@localhost ~]# 
    
  • -c : 统计字节数

    [root@localhost ~]# cat demo.txt | wc -c
    6
    [root@localhost ~]# 
    
  • -w : 统计单词数

    [root@localhost ~]# cat demo.txt | wc -w
    3
    [root@localhost ~]# 
    
    [root@localhost ~]# cat demo.txt | wc -w
    1
    [root@localhost ~]# vim demo.txt 
    [root@localhost ~]# cat demo.txt | wc -w
    3
    [root@localhost ~]# 
    

习题

统计系统文件/etc/services有多少行。
cat /etc/services | wc -l

统计文件/etc/hosts的行数?你能使用几种方法实现?
cat /etc/services | wc -l
wc -l /etc/services

统计文件/etc/services的字节数。你能使用几种方法实现?
wc -c /etc/services
原文地址:https://www.cnblogs.com/zhaokunhao/p/14514067.html