zoukankan      html  css  js  c++  java
  • Linux系统学习之字符处理

    管道

    管道是一种使用非常频繁的通信机制,我们可以使用管道符"|"来连接进程,
    由管道连接起来订单进程可以自动运行,如同有一个数据流一样,所以管道表现为输入输出重定向的一种方法,
    它可以把一个命令的输出内容当作下一个命令的输入内容,两个命令之间只需要管道符连接即可

    使用grep搜索文本

    grep [-ivnc] '需要匹配的字符' 文件名
    #-i 不区分大小写
    #-c 统计包含匹配的行数
    #-n 输出行号
    #-v 反向匹配
    例子:
    [root@Cfhost-170820-UCNK ~]# cat test.txt
    The cat's name is Tom,What's the mouse's name?
    The mouse's NAME is Jerry
    They are good friends
    [root@Cfhost-170820-UCNK ~]# grep 'name' test.txt //搜索含有'name'的句子
    The cat's name is Tom,What's the mouse's name?
    [root@Cfhost-170820-UCNK ~]# grep -i 'name' test.txt //搜索含有'name'的句子,忽略大小写
    The cat's name is Tom,What's the mouse's name?
    The mouse's NAME is Jerry
    [root@Cfhost-170820-UCNK ~]# grep -c 'name' test.txt//含'name'的句子有多少条
    1
    [root@Cfhost-170820-UCNK ~]# grep -ci 'name' test.txt//含'name‘的句子有多少条,大小写可忽略
    2
    [root@Cfhost-170820-UCNK ~]# grep -v 'name' test.txt //搜索不含'name'的句子
    The mouse's NAME is Jerry
    They are good friends
    [root@Cfhost-170820-UCNK ~]# grep -vi 'name' test.txt//搜索不含'name'的句子,大写的NAME也过滤掉
    They are good friends
    [root@Cfhost-170820-UCNK ~]# cat test.txt
    The cat's name is Tom,What's the mouse's name?
    The mouse's NAME is Jerry
    They are good friends
    [root@Cfhost-170820-UCNK ~]# cat test.txt | grep -vi 'name' ? //以上命令都可以使用管道符改写,比如上一个命令可以这样写,意思都是一样的
    They are good friends

    使用sort排序

    sort [-ntkr] 文件名
    
    #-n 采取数字排序
    #-t 指定分隔符
    #-k 指定第几列
    #-r 反向排序
    [root@Cfhost-170820-UCNK ~]# cat sort.txt
    b:3
    c:2
    a:4
    e:5
    d:1
    f:11
    [root@Cfhost-170820-UCNK ~]# cat sort.txt | sort //按字母正向排序
    a:4
    b:3
    c:2
    d:1
    e:5
    f:11
    [root@Cfhost-170820-UCNK ~]# cat sort.txt | sort -r //按字母反向排序
    f:11
    e:5
    d:1
    c:2
    b:3
    a:4
    [root@Cfhost-170820-UCNK ~]# cat sort.txt | sort -t ":" -k 2 -n
    d:1
    c:2
    b:3
    a:4
    e:5
    f:11

    使用uniq删除重复内容

    uniq [-ic]
    #-i 忽略大小写
    #-c 计算重复内容
    
    [root@Cfhost-170820-UCNK ~]# cat uniq.txt | sort | uniq 
    //需要说明的是uniq命令一般需要和sort一起使用,也就是先将文件使用进行sort排序,然后再使用uniq删除重复的内容。单独加上uniq不加sort是没有效果的。
    123
    abc
    
    补充说明:
    [root@Cfhost-170820-UCNK ~]# cat uniq.txt | uniq
    abc
    123
    abc
    123
    
    
    
    
    [root@Cfhost-170820-UCNK ~]# cat uniq.txt | sort | uniq -c 
    //使用-c参数就会在每行前面打印出改行重复的次数
          2 123
    

    使用cutt截取文本、使用tr做文本转换、使用paste做文本合并(还有一个文件分割用split做,这里不再说了,我目前想不到它到底有什么用)

    cut -f 指定列 -d '分隔符'

    [root@Cfhost-170820-UCNK ~]# cat /etc/passwd | cut -f1 -d ':' root bin daemon admin


    tr命令比较简单,其主要作用在于文本转换或删除
    [root@Cfhost-170820-UCNK ~]# cat /etc/passwd | tr '[a-z]' '[A-Z]'
    ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
    BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN

    paste的作用在于将文件按照行进行合并,中间使用tab隔开
    [root@Cfhost-170820-UCNK ~]# cat a.txt
    a b c
    [root@Cfhost-170820-UCNK ~]# cat b.txt
    a b c
    [root@Cfhost-170820-UCNK ~]# paste a.txt b.txt
    a b c    a b c





  • 相关阅读:
    mysql的导出与导入命令的使用
    kendo ui 左侧弹出分享框
    Pytorch离线安装方法
    Python单词接龙小程序
    Shell结束指定名称的进程
    Shell脚本sed命令修改文件的某一行
    Shell中单双引号的区别
    矩阵问题
    泛型通配符详解
    合并链表
  • 原文地址:https://www.cnblogs.com/youcong/p/7816912.html
Copyright © 2011-2022 走看看