Linux 命令(4)-字符处理

156 阅读2分钟

字符处理

  1. | 管道

    [root@localhost ~]# help | more
    
  2. grep 搜索文本

    [root@localhost ~]# grep [-ivnc] '匹配的字符' 文件名
    
    参数含义
    i不区分大小写
    v反向匹配
    n输出行号
    c统计包含匹配的行数
  3. sort 排序

    [root@localhost ~]# sort [-ntkr] 文件名
    
    参数含义
    n采取数字排序
    t指定分隔符
    k指定第几列
    r反向排序
    [root@localhost test]# cat sort.txt
    b:3
    c:2
    a:4
    e:5
    d:1
    f:11
    [root@localhost test]# cat sort.txt | sort
    a:4
    b:3
    c:2
    d:1
    e:5
    f:11
    [root@localhost test]# cat sort.txt | sort -r
    f:11
    e:5
    d:1
    c:2
    b:3
    a:4
    [root@localhost test]# cat sort.txt | sort -t ":" -k 2
    d:1
    f:11
    c:2
    b:3
    a:4
    e:5
    [root@localhost test]# cat sort.txt | sort -t ":" -k 2 -n
    d:1
    c:2
    b:3
    a:4
    e:5
    f:11
    [root@localhost test]#
    
  4. uniq 删除重复内容

    [root@localhost test]# uniq [-ic]
    
    参数含义
    i忽略大小写
    c计算重复行数
    [root@localhost test]# cat uniq.txt | uniq
    asd
    123
    asd
    123
    [root@localhost test]# cat uniq.txt | sort | uniq
    123
    asd
    [root@localhost test]# cat uniq.txt | sort | uniq -c
        2 123
        2 asd
    [root@localhost test]#
    
  5. cut 截取文本

    [root@localhost test]# cut -f指定列 -d'分隔符'
    [root@localhost test]# cut -c指定字符
    
    [root@localhost test]# cat /etc/passwd | cut -f1,6 -d':'
    root:/root
    bin:/bin
    admin:/home/admin
    john:/home/john
    [root@localhost test]#
    
  6. tr 文本替换

    [root@localhost test]# cat /etc/passwd | tr '[a-z]' '[A-Z]'
    ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
    BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
    ADMIN:X:1000:1000::/HOME/ADMIN:/BIN/BASH
    JOHN:X:1001:1001::/HOME/JOHN:/BIN/BASH
    [root@localhost test]#
    [root@localhost test]# cat /etc/passwd | tr -d ':'
    rootx00root/root/bin/bash
    binx11bin/bin/sbin/nologin
    adminx10001000/home/admin/bin/bash
    johnx10011001/home/john/bin/bash
    [root@localhost test]#
    
  7. paste 文本合并

    [root@localhost test]# cat a.txt
    1
    2
    3
    [root@localhost test]# cat b.txt
    a
    b
    c
    [root@localhost test]# paste a.txt b.txt
    1       a
    2       b
    3       c
    [root@localhost test]# paste -d: a.txt b.txt
    1:a
    2:b
    3:c
    [root@localhost test]#
    
  8. spit 分割大文件

    [root@localhost test]# spilt -l 500 test.txt small_file_ # 按行分割
    [root@localhost test]# spilt -b 64m test.txt small_file_ # 按文件大小分割