• shell脚本的文本处理工具


    一、cut

    cut 的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每 一行剪切字节、字符和字段并将这些字节、字符和字段输出。

    1)基本用法

    cut [选项参数] filename

    说明:默认分隔符是制表符

    2)选项参数说明

    选项参数功能
    -f列号,提取第几列
    -d分隔符,按照指定分隔符分割列,默认是制表符“\t”
    -c按字符进行切割 后加加 n 表示取第几列 比如 -c 1

     3)案例实操

    (1)数据准备

    1. [root@hadoop scripts]# vim cut.txt
    2. dong shen
    3. guan zhen
    4. wo wo
    5. lai lai
    6. le le

    (2)切割 cut.txt 第一列

    1. [root@hadoop scripts]# cut -d " " -f 1 cut.txt
    2. dong
    3. guan
    4. wo
    5. lai
    6. le

    (3)切割 cut.txt第二、三列

    1. [root@hadoop scripts]# cut -d " " -f 2,3 cut.txt
    2. shen
    3. zhen
    4. wo
    5. lai
    6. le

    (4)在 /etc/passwd 文件中切割出

    1. [root@hadoop scripts]# cat /etc/passwd | grep bash$ | cut -d ":" -f 1,6,7 #1,6,7表示列数
    2. root:/root:/bin/bash
    3. mysql:/var/lib/mysql:/bin/bash
    4. [root@hadoop scripts]# cat /etc/passwd | grep bash$ | cut -d ":" -f 4- #4-表示4以后列数
    5. 0:root:/root:/bin/bash
    6. 27:MySQL Server:/var/lib/mysql:/bin/bash
    7. [root@hadoop scripts]# cat /etc/passwd | grep bash$ | cut -d ":" -f -4 #-4表示4以前列数
    8. root:x:0:0
    9. mysql:x:27:27

    (5)选取系统 PATH 变量值,第 2 个“:”开始后的所有路径:

    1. [root@hadoop scripts]# echo $PATH | cut -d ":" -f 2
    2. /usr/local/bin
    3. [root@hadoop scripts]# echo $PATH | cut -d ":" -f 10- #第十个以后路径
    4. /usr/soft/hive/bin:/usr/soft/jdk/bin:/usr/soft/phoenix/bin:/usr/soft/scala/bin:/usr/soft/spark/bin:/usr/soft/spark/sbin:/usr/soft/sqoop/bin:/root/bin

    (6)切割 ifconfig 后打印的 IP 地址

    1. [root@hadoop scripts]# ifconfig eth0
    2. eth0 Link encap:Ethernet HWaddr 00:0C:29:02:DA:76
    3. inet addr:192.168.17.151 Bcast:192.168.17.255 Mask:255.255.255.0
    4. inet6 addr: fe80::20c:29ff:fe02:da76/64 Scope:Link
    5. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    6. RX packets:1187588 errors:0 dropped:0 overruns:0 frame:0
    7. TX packets:214810 errors:0 dropped:0 overruns:0 carrier:0
    8. collisions:0 txqueuelen:1000
    9. RX bytes:1297311120 (1.2 GiB) TX bytes:45156985 (43.0 MiB)
    10. [root@hadoop scripts]# ifconfig eth0 | grep Bcast | cut -d " " -f 12 |cut -d ":" -f 2
    11. 192.168.17.151

    二、awk

    一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开 的部分再进行分析处理。

    1)基本用法

    awk [选项参数] ‘/pattern1/{action1} /pattern2/{action2}...’ filename

    pattern:表示 awk 在数据中查找的内容,就是匹配模式

    action:在找到匹配内容时所执行的一系列命令

    2)选项参数说明

    选项参数功能
    -F指定输入文件分隔符
    -v赋值一个用户定义变量

    3)案例实操

    1.搜索 passwd 文件以 root 关键字开头的所有行,并输出该行的第 7 列

    1. [root@hadoop scripts]# cat /etc/passwd | grep ^root | cut -d ":" -f 7
    2. /bin/bash
    3. [root@hadoop scripts]# cat /etc/passwd | awk -F ":" '/^root/ {print $7}'
    4. /bin/bash

     2.搜索 passwd 文件以 root 关键字开头的所有行,并输出该行的第 1 列和第 7 列, 中间以“,”号分割。

    1. [root@hadoop scripts]# cat /etc/passwd | awk -F ":" '/^root/ {print $1","$6","$7}'
    2. root,/root,/bin/bash

     3.只显示/etc/passwd 的第一列和第七列,以逗号分割,且在所有行前面添加列名 user, shell 在最后一行添加"dahaige,/bin/zuishuai"。

    1. [root@hadoop scripts]# cat /etc/passwd | awk -F ":" 'BEGIN{print "user, shell"}{print $1","$7} END{print "end of file"}'
    2. user, shell
    3. root,/bin/bash
    4. bin,/sbin/nologin
    5. daemon,/sbin/nologin
    6. adm,/sbin/nologin
    7. lp,/sbin/nologin
    8. sync,/bin/sync
    9. shutdown,/sbin/shutdown
    10. halt,/sbin/halt
    11. mail,/sbin/nologin
    12. uucp,/sbin/nologin
    13. operator,/sbin/nologin
    14. games,/sbin/nologin
    15. gopher,/sbin/nologin
    16. ftp,/sbin/nologin
    17. nobody,/sbin/nologin
    18. vcsa,/sbin/nologin
    19. saslauth,/sbin/nologin
    20. postfix,/sbin/nologin
    21. sshd,/sbin/nologin
    22. mysql,/bin/bash
    23. ntp,/sbin/nologin
    24. end of file

    4.将 passwd 文件中的用户 id 增加数值 1 并输出

    1. [root@hadoop scripts]# cat /etc/passwd | awk -v i=1 -F ":" '{print $3+i}' #-v是表示变量
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
    7. 6
    8. 7
    9. 8
    10. 9
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 100
    17. 70
    18. 500
    19. 90
    20. 75
    21. 28
    22. 39

    4)awk 的内置变量

    变量说明
    FILENAME文件名
    NR已读的记录数(行号)
    NF浏览记录的域的个数(切割后,列的个数)

    5)案例实操

    1.统计 passwd 文件名,每行的行号,每行的列数

    1. [root@hadoop scripts]# awk -F ":" '{print "文件名:"FILENAME " 行号:"NR "列数: "NF}' /etc/passwd
    2. 文件名:/etc/passwd 行号:1列数: 7
    3. 文件名:/etc/passwd 行号:2列数: 7
    4. 文件名:/etc/passwd 行号:3列数: 7
    5. 文件名:/etc/passwd 行号:4列数: 7
    6. 文件名:/etc/passwd 行号:5列数: 7
    7. 文件名:/etc/passwd 行号:6列数: 7
    8. 文件名:/etc/passwd 行号:7列数: 7
    9. 文件名:/etc/passwd 行号:8列数: 7
    10. 文件名:/etc/passwd 行号:9列数: 7
    11. 文件名:/etc/passwd 行号:10列数: 7
    12. 文件名:/etc/passwd 行号:11列数: 7
    13. 文件名:/etc/passwd 行号:12列数: 7
    14. 文件名:/etc/passwd 行号:13列数: 7
    15. 文件名:/etc/passwd 行号:14列数: 7
    16. 文件名:/etc/passwd 行号:15列数: 7
    17. 文件名:/etc/passwd 行号:16列数: 7
    18. 文件名:/etc/passwd 行号:17列数: 7
    19. 文件名:/etc/passwd 行号:18列数: 7
    20. 文件名:/etc/passwd 行号:19列数: 7
    21. 文件名:/etc/passwd 行号:20列数: 7
    22. 文件名:/etc/passwd 行号:21列数: 7

    2.查询 ifconfig 命令输出结果中的空行所在的行号

    1. [root@hadoop scripts]# ifconfig | grep -n ^$ #不能添加信息
    2. 9:
    3. 18:
    4. [root@hadoop scripts]# ifconfig | awk '/^$/ {print NR}'
    5. 9
    6. 18
    7. [root@hadoop scripts]# ifconfig | awk '/^$/ {print "空行:"NR}' #添加提示信息
    8. 空行:9
    9. 空行:18

    3.切割 IP

    1. [root@hadoop scripts]# ifconfig eth0 | grep Bcast
    2. inet addr:192.168.17.151 Bcast:192.168.17.255 Mask:255.255.255.0
    3. [root@hadoop scripts]# ifconfig | awk '/Bcast/ {print $2}' |cut -d ":" -f 2
    4. 192.168.17.151

  • 相关阅读:
    阿里巴巴面试题- - -Java体系最新面试题(2)
    虚拟DOM与diff算法
    嵌入式分享合集23
    Python与Excel的完美结合:操作技巧与自动化应用
    第2章 Linux的进程管理
    java中json转list和map的嵌套
    leetcode:67. 二进制求和
    Qt / Qt Quick程序打包的一些坑 (三)
    (附源码)ssm停车位共享系统app 毕业设计 041534
    在containerd,将 ‘ctr image‘ 转换成 ‘crictl image‘
  • 原文地址:https://blog.csdn.net/m0_55834564/article/details/126445700