zoukankan      html  css  js  c++  java
  • Linux基础-字符处理命令

    sort
    sort [OPTION] ... [FILE]...
    -r : 倒序
    -n : 按数字排序
    -t : 指定分隔符(默认空格)
    -k : 指定第几列,指定第几列几个字符(指定1,1 3.1,3.3)

    --演示sort用法首先创建一个文件
    [root@localhost ssx-test]# cat >> sort.txt <<EOF
    b:3
    c:2
    a:4
    e:5
    d:1
    f:11
    EOF

    --下面对输出的内容进行排序
    [root@localhost ssx-test]# sort sort.txt
    a:4
    b:3
    c:2
    d:1
    e:5
    f:11
    [root@localhost ssx-test]#

    --可以观察到,sort文件具有一个特点,第一个字符是字母,第三个字符是数字,中间用冒号隔开.
    --这样我们可以使用-t指定分隔符,使用-k指定用于排序的列了
    [root@localhost ssx-test]# sort sort.txt -t ':' -k 2
    d:1
    f:11
    c:2
    b:3
    a:4
    e:5
    [root@localhost ssx-test]#

    --如此排序发现第三列的数字只是别第一位数字
    [root@localhost ssx-test]# sort sort.txt -t ':' -k 2 -n
    d:1
    c:2
    b:3
    a:4
    e:5
    f:11
    [root@localhost ssx-test]# sort sort.txt -t: -k2n
    d:1
    c:2
    b:3
    a:4
    e:5
    f:11
    [root@localhost ssx-test]# sort sort.txt -t: -k2nr
    f:11
    e:5
    a:4
    b:3
    c:2

    --测试案例 对下面内容进行排序

    cat >test.txt <<EOF
    192.168.3.1 00:0F:AF:81:19:1F
    192.168.3.2 00:0F:AF:85:6C:25
    192.168.3.3 00:0F:AF:85:70:42
    192.168.2.20 00:0F:AF:85:55:DE
    192.168.2.21 00:0F:AF:85:6C:09
    192.168.2.22 00:0F:AF:85:5C:41
    192.168.0.151 00:0F:AF:85:6C:F6
    192.168.0.152 00:0F:AF:83:1F:65
    192.168.0.153 00:0F:AF:85:70:03
    192.168.1.10 00:30:15:A2:3B:B6
    192.168.1.11 00:30:15:A3:23:B7
    192.168.1.12 00:30:15:A2:3A:A1
    192.168.1.1 00:0F:AF:81:19:1F
    192.168.2.2 00:0F:AF:85:6C:25
    192.168.3.3 00:0F:AF:85:70:42
    192.168.2.20 00:0F:AF:85:55:DE
    192.168.1.21 00:0F:AF:85:6C:09
    192.168.2.22 00:0F:AF:85:5C:41
    192.168.0.151 00:0F:AF:85:6C:F6
    192.168.1.152 00:0F:AF:83:1F:65
    192.168.0.153 00:0F:AF:85:70:03
    192.168.3.10 00:30:15:A2:3B:B6
    192.168.1.11 00:30:15:A3:23:B7
    192.168.3.12 00:30:15:A2:3A:A1
    EOF

    --针对第三列的第一个字符,第四列的第一个字符到第三个字符排序才是正确的
    -k
    start_first.last,end_first.last

    [root@localhost ssx-test]# sort test.txt -t. -k3.1,3.1 -k4.1,4.3 -n
    192.168.0.151 00:0F:AF:85:6C:F6
    192.168.0.151 00:0F:AF:85:6C:F6
    192.168.0.152 00:0F:AF:83:1F:65
    192.168.0.153 00:0F:AF:85:70:03
    192.168.0.153 00:0F:AF:85:70:03
    192.168.1.1 00:0F:AF:81:19:1F
    192.168.1.10 00:30:15:A2:3B:B6
    192.168.1.11 00:30:15:A3:23:B7
    192.168.1.11 00:30:15:A3:23:B7
    192.168.1.12 00:30:15:A2:3A:A1
    192.168.1.21 00:0F:AF:85:6C:09
    192.168.1.152 00:0F:AF:83:1F:65
    192.168.2.2 00:0F:AF:85:6C:25
    192.168.2.20 00:0F:AF:85:55:DE
    192.168.2.20 00:0F:AF:85:55:DE
    192.168.2.21 00:0F:AF:85:6C:09
    192.168.2.22 00:0F:AF:85:5C:41
    192.168.2.22 00:0F:AF:85:5C:41
    192.168.3.1 00:0F:AF:81:19:1F
    192.168.3.2 00:0F:AF:85:6C:25
    192.168.3.3 00:0F:AF:85:70:42
    192.168.3.3 00:0F:AF:85:70:42
    192.168.3.10 00:30:15:A2:3B:B6
    192.168.3.12 00:30:15:A2:3A:A1
    [root@localhost ssx-test]#

    uniq
    --使用uniq删除重复内容
    如果文件中有多行完全相同的内容,当前是希望能删除重复的行,同时还可以统计出完全相
    同的行出现的总次数,那么就可以使用uniq命令解决这个问题(但是必须配合sort使用)

    uniq [OPEION] ... [INPUT [OUTPUT]]
    -c 计算重复的行

    --演示uniq的用法,首先创建一个文件
    [root@localhost ssx-test]# cat > uniq.txt << EOF

    abc
    123
    abc
    123
    EOF
    [root@localhost ssx-test]# cat uniq.txt
    abc
    123
    abc
    123

    --uniq需要和sort配合使用
    [root@localhost ssx-test]# uniq uniq.txt
    abc
    123
    abc
    123

    --uniq需要和sort一起使用, 先使用sort排序, 让重复内容连续在一起
    [root@localhost ssx-test]# sort uniq.txt
    123
    123
    abc
    abc

    --使用uniq去除相邻重复的行
    [root@localhost ssx-test]# sort uniq.txt | uniq
    123
    abc

    --使用-c参数, 能统计出文件中每行内容重复的次数
    [root@localhost ssx-test]# sort uniq.txt | uniq -c
    2 123
    2 abc
    [root@localhost ssx-test]#

    --配合使用sort和uniq命令对上述ip/mac地址排序去重,并统计重复内容出现的次数
    [root@localhost ssx-test]# sort test.txt -t. -k3.1,3.1 -k4.1,4.3 -n | uniq -c
    2 192.168.0.151 00:0F:AF:85:6C:F6
    1 192.168.0.152 00:0F:AF:83:1F:65
    2 192.168.0.153 00:0F:AF:85:70:03
    1 192.168.1.1 00:0F:AF:81:19:1F
    1 192.168.1.10 00:30:15:A2:3B:B6
    2 192.168.1.11 00:30:15:A3:23:B7
    1 192.168.1.12 00:30:15:A2:3A:A1
    1 192.168.1.21 00:0F:AF:85:6C:09
    1 192.168.1.152 00:0F:AF:83:1F:65
    1 192.168.2.2 00:0F:AF:85:6C:25
    2 192.168.2.20 00:0F:AF:85:55:DE
    1 192.168.2.21 00:0F:AF:85:6C:09
    2 192.168.2.22 00:0F:AF:85:5C:41
    1 192.168.3.1 00:0F:AF:81:19:1F
    1 192.168.3.2 00:0F:AF:85:6C:25
    2 192.168.3.3 00:0F:AF:85:70:42
    1 192.168.3.10 00:30:15:A2:3B:B6
    1 192.168.3.12 00:30:15:A2:3A:A1
    [root@localhost ssx-test]#

    统计一下自己使用的命令的排序
    提示:
    history

    cut
    https://www.cnblogs.com/Spiro-K/p/6361646.html
    --文件内容查看
    -- 显示行中的指定部分,删除文件中的指定字段
    -- 显示文件的内容,类似于下的type命令

    --说明
    -- 该命令有两项功能,其一是用来显示文件的内容,他依次读取由参数file所指明的文件,
    将它们的内容输出到标准输出上;其二是连接两个或多个文件,如cut f1 f2 > f3将把
    文件f1和f2的内容合并起来,然后通过输出重定向符">"的作用,将它们放入文件f3中.
    -- 当文件较大时,文件在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容.因此,一
    般使用more命令分屏显示
    -- 为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏
    -- 按Ctrl+C 中断键可以终止该命令的执行,并且返回Shell提示符状态

    --语法
    -- cut [OPTION] [参数]

    --OPTION
    -b : 仅显示行中指定直接范围的内容
    -c : 仅显示行中指定范围的字符
    -d : 指定字段的分隔符,默认的字段分隔符为"TAB"
    -f : 显示指定字段的内容
    -n : 与"b"选项连用,不分割多字节字符
    --complement : 补足被选择的字节/字符或字段
    --out--delimiter=<字段分隔符> : 指定输出内容是的字段分隔符
    --help : 显示指定的帮助信息
    --version : 显示指令的版本信息

    cut示例
    --例如有一个学生报表信息,包含No、Name、Mark、Percent:
    cat > stu_info.txt << EOF
    No Name Mark Percent
    01 tom 69 91
    02 jack 71 87
    03 alex 68 98
    EOF

    --使用-f选项提取指定字段
    [root@bogon ssx-test]# cut -d ' ' -f 1 stu_info.txt
    No
    01
    02
    03
    [root@bogon ssx-test]#

    --使用--completement选项提取指定字段之外的列(打印除了第二列之外的列):
    [root@bogon ssx-test]# cut -d ' ' -f 1 --complement stu_info.txt
    Name Mark Percent
    tom 69 91
    jack 71 87
    alex 68 98
    [root@bogon ssx-test]#

    --使用-d选项指定字段分隔符
    cat > stu_info123.txt << EOF
    No Name Mark Percent
    01,tom,69,91
    02,jack,71,87
    03,alex,68,98
    EOF

    [root@bogon ssx-test]# cut -d ',' -f 1 --complement stu_info123.txt
    No Name Mark Percent
    tom,69,91
    jack,71,87
    alex,68,98
    [root@bogon ssx-test]#

    --指定字段的字符或者字节范围

    cut命令可以将一串字符作为列来显示,字符字段的记法:
    N-:从第N个字节、字符、字段到结尾;
    N-M:从第N个字节、字符、字段到第M个(包括M在内)字节、字符、字段;
    -M:从第1个字节、字符、字段到第M个(包括M在内)字节、字符、字段。

    上面是记法,结合下面选项将摸个范围的字节、字符指定为字段:
    -b 表示字节;
    -c 表示字符;
    -f 表示定义字段。

    --创建一个文件

    cat > test.txt << EOF
    abcdefghijklmnopqrstuvwxyz
    abcdefghijklmnopqrstuvwxyz
    abcdefghijklmnopqrstuvwxyz
    abcdefghijklmnopqrstuvwxyz
    abcdefghijklmnopqrstuvwxyz
    EOF

    --打印第1个到第3个字符
    [root@bogon ssx-test]# cut -c1-3 test.txt
    abc
    abc
    abc
    abc
    abc
    [root@bogon ssx-test]#

    --打印前2个字符
    [root@bogon ssx-test]# cut -c-2 test.txt
    ab
    ab
    ab
    ab
    ab
    [root@bogon ssx-test]#

    --打印从第5个字符开始到结尾
    [root@bogon ssx-test]# cut -c5- test.txt
    efghijklmnopqrstuvwxyz
    efghijklmnopqrstuvwxyz
    efghijklmnopqrstuvwxyz
    efghijklmnopqrstuvwxyz
    efghijklmnopqrstuvwxyz
    [root@bogon ssx-test]#

    awk
    使用awk命令截取一个字段
    -F : 指定分隔符
    '{print $n}' : 取出第几列 $1 $2 $3
    使用history

    1. 采集数据
    2. 排序
    3. 去重
    4. 统计
    5. 排序
      [root@bogon ssx-test]# history | awk '{print $2}' | sort | uniq -c | sort -nr
      26 ls
      22 sort
      22 cat
      21 locate
      20 cut
      12 ll
      12
      9 history
      9 cd
      8 whereis
      6 wget
      5 which
      5 systemctl
      5 rm
      5 init
      5 EOF
      5 clear
      4 sz
      4 pwd
      4 abc
      3 vi
      3 ifconfig
      3 123
      2 rz
      2 rpm
      2 getenforce
      2 f:11
      2 e:5
      2 d:1
      2 c:2
      2 b:3
      2 a:4
      2 192.168.3.3
      2 192.168.2.22
      2 192.168.2.20
      2 192.168.1.11
      2 192.168.0.153
      2 192.168.0.151
      1 yum
      1 w
      1 uniq
      1 touch
      1 setenforce
      1 [root@server
      1 No
      1 Name
      1 mkdir
      1 man
      1 his
      1 192.168.3.2
      1 192.168.3.12
      1 192.168.3.10
      1 192.168.3.1
      1 192.168.2.21
      1 192.168.2.2
      1 192.168.1.21
      1 192.168.1.152
      1 192.168.1.12
      1 192.168.1.10
      1 192.168.1.1
      1 192.168.0.152
      1 03
      1 02
      1 01
      [root@bogon ssx-test]#
  • 相关阅读:
    妹妹
    小猴和北极熊
    盛趣->盛大
    运维
    操之过急
    修马路
    博人传
    醉酒
    【跨域】SpringBoot跨域,拦截器中,第一次获取的请求头为NULL,发送两次请求的处理方式
    【Linux】Linux安装Tomcat
  • 原文地址:https://www.cnblogs.com/s-sx/p/11840536.html
Copyright © 2011-2022 走看看