十一、文本处理工具、综合应用案例

一、文本处理工具

1 cut

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

1)基本用法

cut [选项参数] filename 说明:默认分隔符是制表符

2)选项参数说明

在这里插入图片描述
3)案例实操
(1)数据准备

[atguigu@hadoop101 shells]$ touch cut.txt 
[atguigu@hadoop101 shells]$ vim cut.txt 
dong shen 
guan zhen 
wo wo 
lai lai 
le le

(2)切割 cut.txt 第一列

[atguigu@hadoop101 shells]$ cut -d " " -f 1 cut.txt
 dong 
 guan 
 wolai 
 le

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

[atguigu@hadoop101 shells]$ cut -d " " -f 2,3 cut.txt 
shen 
zhen 
	wolai 
	le
[root@hadoop100 scripts]# cat /etc/passwd | grep bash$
root:x:0:0:root:/root:/bin/bash
string-int:x:1000:1000:String-int:/home/string-int:/bin/bash
[root@hadoop100 scripts]# cat /etc/passwd | grep bash$ | cut -d ":" -f 1,6,7
root:/root:/bin/bash
string-int:/home/string-int:/bin/bash
[root@hadoop100 scripts]# 

(4)在 cut.txt 文件中切割出 guan

[atguigu@hadoop101 shells]$ cat cut.txt |grep guan | cut -d " " -f 1 
guan

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

[atguigu@hadoop101 shells]$ echo $PATH 
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/atguigu/.local/bin:/ home/atguigu/bin
[atguigu@hadoop101 shells]$ echo $PATH | cut -d ":" -f 3-
/usr/local/sbin:/usr/sbin:/home/atguigu/.local/bin:/home/atguigu/bin

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

[atguigu@hadoop101 shells]$ ifconfig ens33 | grep netmask | cut -d " " -f 10 
192.168.111.101

2 awk

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

1)基本用法
[root@hadoop100 scripts]# which awk #查看是否有awk
/usr/bin/awk

awk [选项参数]/pattern1/{action1} /pattern2/{action2}...’ filename 
pattern:表示 awk 在数据中查找的内容,就是匹配模式 
action:在找到匹配内容时所执行的一系列命令
2)选项参数说明

在这里插入图片描述

3)案例实操

(1)数据准备

[atguigu@hadoop101 shells]$ sudo cp /etc/passwd ./ 
passwd 数据的含义 
用户名:密码(加密过后的):用户 id:组 id:注释:用户家目录:shell 解析器

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

[atguigu@hadoop101 shells]$ awk -F : '/^root/{print $7}' passwd 
/bin/bash

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

[atguigu@hadoop101 shells]$ awk -F : '/^root/{print $1","$7}' passwd 
root,/bin/bash

注意:只有匹配了 pattern 的行才会执行 action。

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

[atguigu@hadoop101 shells]$ awk -F : 'BEGIN{print "user, shell"} {print $1","$7} 
END{print "dahaige,/bin/zuishuai"}' passwd 
user, shell 
root,/bin/bash 
bin,/sbin/nologin 
。。。 
atguigu,/bin/bash 
dahaige,/bin/zuishuai

注意:BEGIN 在所有数据读取行之前执行;END 在所有数据执行之后执行。

(5)将 passwd 文件中的用户 id 增加数值 1 并输出

[atguigu@hadoop101 shells]$ awk -v i=1 -F : '{print $3+i}' passwd 
1
2
3
4
4)awk 的内置变量

在这里插入图片描述

5)案例实操

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

[atguigu@hadoop101 shells]$ awk -F : '{print "filename:" FILENAME ",linenum:" 
NR ",col:"NF}' /etc/passwd
filename:passwd,linenum:1,col:7 
filename:passwd,linenum:2,col:7 
filename:passwd,linenum:3,col:7 
...
cat /etc/passwd | awk -F : '{print "filename:" FILENAME ",linenum:" NR ",col:"NF}'

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

[atguigu@hadoop101 shells]$ ifconfig | awk '/^$/{print NR}' 
9
18
26

(3)切割 IP

[atguigu@hadoop101 shells]$ ifconfig ens33 | awk '/netmask/ {print $2}' 
192.168.6.101

二、综合应用案例

1 归档文件

实际生产应用中,往往需要对重要数据进行归档备份。

需求:实现一个每天对指定目录归档备份的脚本,输入一个目录名称(末尾不带/), 将目录下所有文件按天归档保存,并将归档日期附加在归档文件名上,放在/root/archive 下。

这里用到了归档命令:tar

后面可以加上-c 选项表示归档,加上-z 选项表示同时进行压缩,得到的文件后缀名 为.tar.gz。

脚本实现如下:

[root@hadoop100 scripts]# vim daily_archive.sh #创建

#!/bin/bash


# 首先判断输入的参数个数是否为1
if [ $# -ne 1 ]
then
        echo "参数的个数错误!应该输入一个参数,作为归档目录"
        exit
fi


# 从参数中获取目录名称

if [ -d $1 ]
then
        echo
else
        echo
        echo "目录不存在!"
        echo
        exit
fi

DIR_NAME=$(basename $1)
DIR_PATH=$(cd $(dirname $1); pwd)

# 获取当前日期
DATE=$(date +%y%m%d)

#定义生成的归档文件名称
FILE=archive_$(DIR_NAME)_$DATE.tar.gz
DEST=/root/archive/$FILE

# 开始归档目录文件

echo "开始归档。。。"
echo

tar -czf $DEST $DIR_PATH/$DIR_NAME



if [ $? -eq 0 ]
then

        echo
        echo "归档成功!"
        echo "归档文件为:$DEST"
        echo
else

        echo "归档出现问题!"
        echo
fi

exit


[root@hadoop100 scripts]# chmod u+x daily_archive.sh #设置执行权限
[root@hadoop100 scripts]# ./daily_archive.sh ../scripts
 
开始归档。。。

tar: 从成员名中删除开头的“/”
tar: /root/scripts/scripts:无法 stat: 没有那个文件或目录
tar (child): /root/archive/archive__220912.tar.gz:无法 open: 没有那个文件或目录
归档出现问题!

[root@hadoop100 scripts]# mkdir /root/archive #创建archive 目录

[root@hadoop100 scripts]# ./daily_archive.sh ../scripts

./daily_archive.sh:行31: DIR_NAME: 未找到命令
开始归档。。。

tar: 从成员名中删除开头的“/”

归档成功!
归档文件为:/root/archive/archive__220912.tar.gz

[root@hadoop100 scripts]#  
[root@hadoop100 scripts]# crontab -l
no crontab for root
[root@hadoop100 scripts]# crontab -e

0 2 * * * /root/scripts/daily_archive.sh /root/scripts

[root@hadoop100 scripts]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@hadoop100 scripts]# crontab -l
0 2 * * * /root/scripts/daily_archive.sh /root/scripts

[root@hadoop100 scripts]# 


2 发送消息

我们可以利用 Linux 自带的 mesg 和 write 工具,向其它用户发送消息。

需求:实现一个向某个用户快速发送消息的脚本,输入用户名作为第一个参数,后面直 接跟要发送的消息。脚本需要检测用户是否登录在系统中、是否打开消息功能,以及当前发 送消息是否为空。

[root@hadoop100 scripts]# who am i
root     pts/0        2022-09-12 13:47 (192.168.174.1)
[root@hadoop100 scripts]# who
root     pts/0        2022-09-12 13:47 (192.168.174.1)
[root@hadoop100 scripts]# who
root     pts/0        2022-09-12 13:47 (192.168.174.1)
string-int pts/1        2022-09-12 15:30 (192.168.174.1)
[root@hadoop100 scripts]# mesg #查看当前用户的通讯功能是否打开
is y
[root@hadoop100 scripts]# who -T #查看所有用户的通讯功能是否打开
root     + pts/0        2022-09-12 13:47 (192.168.174.1)
string-int + pts/1        2022-09-12 15:30 (192.168.174.1)
[root@hadoop100 scripts]# 
[root@hadoop100 scripts]# mesg n #关闭
[root@hadoop100 scripts]# who -T
root     - pts/0        2022-09-12 13:47 (192.168.174.1)
string-int + pts/1        2022-09-12 15:30 (192.168.174.1)
[root@hadoop100 scripts]# mesg y #打开
[root@hadoop100 scripts]# 
[root@hadoop100 scripts]# who -T
root     + pts/0        2022-09-12 13:47 (192.168.174.1)
string-int + pts/1        2022-09-12 15:30 (192.168.174.1)
[root@hadoop100 scripts]# 
[root@hadoop100 scripts]# write string-int pts/1 #向sting-int 的pts/1控制台发送消息
hi ,^H^Hstring-int  # 信息的内容

[string-int@hadoop100 ~]$ 
Message from root@hadoop100 on pts/0 at 15:36 ...  #root 发来的消息
hi ,^H^Hstring-int #内容

脚本实现如下:

[root@hadoop100 scripts]# vim send_msg.sh #




#!/bin/bash

#查看用户是否登陆
# -i 忽略大小写。 -m 1 只要第一行
login_user=$(who | grep -i -m 1 $1 | awk '{print $1}')
# -z 判断字符串是否为空
if [ -z $login_user ]
then

        echo "$1 不在线!"
        echo "脚本退出。。。"
        exit
fi

#查看用户是否开启消息功能
is_allowed=$(who -T | grep -i -m 1 $1 | awk '{print $2}')
# -z 判断字符串是否为空
if [ $is_allowed != "+" ]
then

        echo "$1 没有开启消息功能"
        echo "脚本退出。。。"
        exit
fi
#确认是否有消息发送 
if [ -z $2 ]
then

        echo "没有消息发送"
        echo "脚本退出。。。"
        exit
fi

#获取要发送的消息
whole_msg=$(echo $* | cut -d " " -f 2-)
#获取用户登录的终端
user_terminal=$(who | grep -i -m 1 $1 | awk '{print $2}')

#写入要发送的消息
echo $whole_msg | write $login_user $user_terminal

if [ $? != 0 ]
then
        echo "发送失败"
else
        echo "发送成功"
fi

exit



[root@hadoop100 scripts]# ./send_msg.sh 
用法: grep [选项]... PATTERN [FILE]...
试用‘grep --help’来获得更多信息。
 不在线!
脚本退出。。。
[root@hadoop100 scripts]# ./send_msg.sh string-int
没有消息发送
脚本退出。。。
[root@hadoop100 scripts]# ./send_msg.sh string-int hi,strng-int
发送成功
[root@hadoop100 scripts]# 

[string-int@hadoop100 ~]$ 
Message from root@hadoop100 on pts/0 at 19:21 ...
hi,strng-int
EOF

1 百度&考满分

问题:Linux 常用命令

参考答案:find、df、tar、ps、top、netstat 等。(尽量说一些高级命令)

2 瓜子二手车

问题:Linux 查看内存、磁盘存储、io 读写、端口占用、进程等命令

答案:
1、查看内存:top
2、查看磁盘存储情况:df -h
3、查 看磁盘 IO 读写情况:iotop(需要安装一下:yum install iotop)、 iotop -o(直接查看输出比较高的磁盘读写程序)
4、查看端口占用情况:netstat -tunlp | grep 端口号
5、查看进程:ps -aux

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值