cut

作者: 鹿慕叶 | 来源:发表于2016-11-16 00:02 被阅读0次

    使用cut选定字段

    cut命令是用来剪下文本文件里的数据,文本文件可以是字段类型或是字符类型。后一种数据类型在遇到需要从文件里剪下特定的列时,特别方便。请注意:一个制表字符在此被视为单个字符。

    举例来说,下面的命令可显示系统上每个用户的登录名称及其全名:

    root@kali:~# cat /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
    ...
    
    root@kali:~# cut -d : -f 1,5 /etc/passwd
    root:root
    daemon:daemon
    ...
    

    通过选择其他字段编号,还可以取出每个用户的根目录:

    root@kali:~# cut -d : -f 6 /etc/passwd
    /root
    /usr/sbin
    ...
    

    更多实例

    例如有一个学生报表信息,包含No、Name、Mark

    root@kali:~/lab/cache# cat cut.txt 
    NO  NAME    MARK
    01  tom 69
    02  lucy    99
    03  lomoye  100
    

    使用 -f 选项提取指定字段

    root@kali:~/lab/cache# cut -f 1 cut.txt 
    NO
    01
    02
    03
    root@kali:~/lab/cache# cut -f 2 cut.txt 
    NAME
    tom
    lucy
    lomoye
    root@kali:~/lab/cache# cut -f 3 cut.txt 
    MARK
    69
    99
    100
    

    以上内容选材于shell脚本学习指南以及linux命令大全
    代码部分属于自己的实践内容

    相关文章

      网友评论

          本文标题:cut

          本文链接:https://www.haomeiwen.com/subject/mvmlpttx.html