zoukankan      html  css  js  c++  java
  • linux(centos8):用cut显示文本内容的指定列

    一,cut命令的用途

    从一个文本文件或者文本流中提取文本列

    分别用: 字节、字符、字段 作为单位进行提取

    说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

             对应的源码可以访问这里获取: https://github.com/liuhongdi/

    说明:作者:刘宏缔 邮箱: 371125307@qq.com

    二,查看cut命令所属的包

    [root@blog ~]$ whereis cut
    cut: /usr/bin/cut /usr/share/man/man1/cut.1.gz /usr/share/man/man1p/cut.1p.gz
    
    [root@blog ~]$ rpm -qf /usr/bin/cut
    coreutils-8.30-6.el8.x86_64

    如果提示找不到命令或命令被误删除,

    可以用dnf安装

    [root@blog ~]$ dnf install coreutils

    三,查看cut命令的版本和帮助

    1,查看版本

    [root@blog ~]$ cut --version
    cut (GNU coreutils) 8.30
    Copyright (C) 2018 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

    2,查看帮助

    [root@blog ~]$ cut --help

    3,查看手册

    [root@blog ~]$ man cut

    四,cut命令在运维中的使用例子:

    1,显示nginx日志中所有的ip

    # -d:指定分隔字段的分隔符,默认的分隔符是tab

    #-f: 指定显示第几个字段

    [root@blog nginxlogs]$ cut -d ' ' -f 1 file_meet.access_log
    106.15.200.123
    47.101.200.88
    ...

    类似的还有:

    第7列是url(使用空格作分隔符)

    [root@blog nginxlogs]$ cut -d ' ' -f 7 file_meet.access_log
    /
    /web2/images/h4.png
    /web2/images/h10.png 
    ...

    用双引号做分隔符,第6列是user agent

    [root@blog nginxlogs]$ cut -d '"' -f 6 file_meet.access_log | more
    Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0
    Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0
    ...

    2,列出所有有权登录bash的用户

    # -d:指定分隔字段的分隔符,默认的分隔符是tab

    #-f: 指定显示第几个字段

    [root@blog ~]$ grep '/bin/bash' /etc/passwd | cut -d ':' -f 1,7
    root:/bin/bash
    webop:/bin/bash

    说明:上面的命令效果等同于:

    #--complement:  显示-f指定字段以外的其他字段

    [root@blog ~]$ grep '/bin/bash' /etc/passwd | cut -d ':' -f 2,3,4,5,6 --complement

    也等同于

    [root@blog ~]$ grep '/bin/bash' /etc/passwd | cut -d ':' -f 2-6 --complement

    3,打印每个分区和使用的占比

    #sed '1d' :去掉第一行的表头

    #tr -s:--squeeze-repeats:缩减连续重复的字符成指定的单个字符

    [root@blog ~]$ df -h | sed '1d' | tr -s ' ' | cut -d ' ' -f 1,5
    devtmpfs 0%
    tmpfs 0%
    tmpfs 1%
    tmpfs 0%
    /dev/vda1 15%
    /dev/vdb1 1%
    tmpfs 0%

    说明:df 命令输出的部分空格较多,数量不一致,

              我们用tr做一下压缩,这样方便cut读取

    4,打印每个分区和使用的占比,字段之间用-分隔

    #--output-delimiter='-' 输出时的分隔符也可以指定

    #tr -d '%'   去掉百分比符号

    [root@blog ~]$ df -h | sed '1d' | tr -s ' ' | cut -d ' ' -f 1,5 --output-delimiter='-' | tr -d '%'
    devtmpfs-0
    tmpfs-0
    tmpfs-1
    tmpfs-0
    /dev/vda1-15
    /dev/vdb1-1
    tmpfs-0

    5,用cut截取每行的前5个字符

    #-c:截取指定位置的字符

    [root@blog ~]$ cut -c1-5 /etc/passwd 

    6,其他参数:

    -s:--only-delimited 不包含分隔符的行直接不显示 ( do not print lines not containing delimiters)

    -b: 以字节为单位进行分割

    五,查看centos的版本

    [root@blog nginxlogs]$ cat /etc/redhat-release
    CentOS Linux release 8.0.1905 (Core)
  • 相关阅读:
    snmp实用篇
    使用snmp4j实现Snmp功能(三)
    使用snmp4j实现Snmp功能(二)
    使用snmp4j实现Snmp功能(一)
    “百度杯”CTF比赛 九月场_Test(海洋cms前台getshell)
    “百度杯”CTF比赛 九月场_123(文件备份,爆破,上传)
    “百度杯”CTF比赛 九月场_SQLi
    “百度杯”CTF比赛 九月场_SQL
    “百度杯”CTF比赛 九月场_再见CMS(齐博cms)
    “百度杯”CTF比赛 九月场_YeserCMS
  • 原文地址:https://www.cnblogs.com/architectforest/p/12893450.html
Copyright © 2011-2022 走看看