Shell中的文本处理:grep 文本过滤命令、sed 行编辑器

1.grep 文本过滤命令
(1)grep命令的简介

##全面搜索研究正则表达式并显示出来
grep 命令(Global search regular expression and print out the line)是一种强大的文本搜索工具,
根据用户指定的‘模式’对目标文本进行匹配检查,打印匹配到的行。由正则表达式或者字符及基本文本字符所编写的过滤条件

(2)grep命令的基本用法

grep   + 参数  + 文件名

具体参数含义:
      -i	##忽略字母大小写
      -v	##条件取反
      -c	##统计匹配行数
      -q	##静默,无任何输出
      -n	##显示匹配结果所在的行号
      -E    ##解释PATTERN作为扩展正则表达式,(grep -E 就相当于 egrep)

实验:

[root@localhost mnt]# vim grep_test
[root@localhost mnt]# cat grep_test 

在这里插入图片描述

## -i 表示忽略字母大小写(ignore);忽略字母大小写过滤出grep_test文件种含有root字符的行信息
[root@localhost mnt]# grep -i root grep_test 

在这里插入图片描述

## -v 表示条件取反(revert);过滤出不含有root字符的行信息
[root@localhost mnt]# grep -v root grep_test 
Root:linux:westos
rawt:rooot:wetless
## -c 表示统计匹配行数(count)
[root@localhost mnt]# grep -c root grep_test 

在这里插入图片描述

## -n 表示显示匹配结果及其所在行号
[root@localhost mnt]# grep -n root grep_test 

在这里插入图片描述
1)grep中字符的匹配位置设定

^           ##表示以……开头    
$           ##表示以……结尾    
\<  \>      ##表示不做任何拓展
[]          ##匹配中括号内的任意字符
[^]         ##匹配不在括号内的任意字符

##过滤出含有root关键字的行信息
[root@localhost mnt]# grep root grep_test
在这里插入图片描述
##过滤出以root开头的行信息
[root@localhost mnt]# grep ^root grep_test
在这里插入图片描述
##关键字root前后均不做拓展
[root@localhost mnt]# grep ‘<root>’ grep_test
在这里插入图片描述
(4)egrep命令

  • 实质上: egrep 命令 即为 grep -E 命令

##过滤以root关键字开头的前3行信息
[root@localhost mnt]# grep -m3 ‘^root’ grep_test
在这里插入图片描述
##过滤空行;-v表示取反
[root@localhost mnt]# egrep -v ‘.’ grep_test
[root@localhost mnt]# egrep ‘^$’ grep_test
在这里插入图片描述
2.sed 行编辑器

sed    ##一次处理一行内容,处理时,把当前的行存储在临时缓冲区,处理完后,输送到屏幕(stream editor)

一般格式: sed [参数] ‘命令’ fileame

具体参数含义:
	      p	      ##显示
	      d	      ##删除
	      a	      ##添加
	      c	      ##替换
	      w       ##写入  
	      i	      ##插入

(1) p 模式操作(显示)

[root@localhost mnt]# cat /etc/hosts
##显示含有:的行
[root@localhost mnt]# sed -n '/:/p' /etc/hosts

在这里插入图片描述

##显示以#开头的行
[root@localhost mnt]# sed -n '/^#/p' /etc/fstab
##不显示以#开头的行
[root@localhost mnt]# sed -n '/^#/!p' /etc/fstab

在这里插入图片描述

##显示2-6行
[root@localhost mnt]# sed -n '2,6p' /etc/fstab
##仅显示第2和第6行
[root@localhost mnt]# sed -n '2p;6p' /etc/fstab

在这里插入图片描述
(2) d模式操作(删除)
##删除以#开头的行
[root@localhost mnt]# sed ‘/^#/d’ /etc/fstab
在这里插入图片描述
##删除1-4行
[root@localhost mnt]# sed ‘1,4d’ /etc/fstab
在这里插入图片描述
(3) a操作模式(添加)
在这里插入图片描述
(4) c操作模式(替换)

##c表示替换
[root@localhost mnt]# sed ‘/hello/chello word!’ westos
在这里插入图片描述

安装apache并更改apache的默认端口为8080

[root@localhost mnt]# vim /etc/httpd/conf/httpd.conf
在这里插入图片描述

##-i表示更改原文件内容;c表示替换
[root@localhost mnt]# sed -i '/Listen 80/cListen 8080' /etc/httpd/conf/httpd.conf
[root@localhost mnt]# vim /etc/httpd/conf/httpd.conf 

在这里插入图片描述
编写脚本:

[root@localhost mnt]# vim  apache_8080.sh 
#####################
read -p "please input a port:" a     ##接收用户输入信息

yum install -y apache &> /dev/null
systemctl start httpd
sed -i '/^Listen/cListen '$a'' /etc/httpd/conf/httpd.conf   ##将以Listen开头的替换为Listen 和你所想要更改的端口号
echo "port has changed!"
 
setenforce 0                         ##selinux会影响端口的更改
systemctl stop firewalld   
systemctl restart httpd

在这里插入图片描述

[root@localhost mnt]# sh apache_8080.sh 
please input a port:8080
port has changed!
##发现apache的端口确实更改了
[root@localhost mnt]# vim /etc/httpd/conf/httpd.conf 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值