3天玩转shell--6.sed 和grep用法

简介: 本文将通过shell代码示例,简单通俗的讲解shell。通过执行代码和运行结果反向掌握shell编程方法。准备一台低配的阿里云ECS Linux环境,跟着教程走起,本文比较适合shell小白。

一、这节课掌握如下几个知识点

【1】掌握sed字符替换技巧
【2】掌握grep字符过滤技巧

二、sed的用法

先根据附件中的文件创建好脚本执行需要的样例,6.example.txt和6.example2.txt。然后根据注释执行,或执行脚本。
#!/bin/bash
#6.sh v1
#create by maoge 2020.05.01
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
export LANG=zh_CN.UTF-8
export PATH

\cp 6.example.txt 6.example.txt.old #复制一个txt文本出来进行修改
#1.常规字符替换
echo "============常规字符替换===="
echo "aabbccaa"|sed 's#aa#kk#'   #将第一次出现的字符aa替换为kk
echo "aabbccaa"|sed 's#aa#kk#g'  #将字符aa全部替换为kk
echo "aabbccaa"|sed 's/aa/kk/g'  #将字符aa全部替换为kk
sed -i 's#three#333#g' 6.example.txt #将文件中的three替换成333
cat 6.example.txt
echo "==============="
sed -i 's/\#TCPKeepAlive/TCPKeepAlive/g' 6.example.txt   #取消sshd前面的注释
cat 6.example.txt
echo "==============="
sed -i 's/^UseLogin/#UseLogin/g' 6.example.txt   #注释以sshd开头的字符
cat 6.example.txt
echo "==============="
sed -i '/Unix/d' 6.example.txt                #删除匹配Unix的行
cat 6.example.txt
echo "==============="
sed -i '/^$/d' 6.example.txt                #删除空白的行
cat 6.example.txt
echo "==============="
sed -i 's/^/#&/g' 6.example.txt    #在文件6.example.txt中的每行前面插入#
cat 6.example.txt
echo "==============="
sed -i 's/^#//g'  6.example.txt    #将行首的#替换为空
cat 6.example.txt
echo "==============="
sed 's/$/&ok/g' 6.example.txt    #在每行的行尾添加字符ok

#2.正则替换字符
echo "==============="
sed -i 's#[0-9][0-9]\+#[0-9]#g' 6.example.txt   替换连续的数字
cat 6.example.txt

#3.匹配字符打印
echo "============匹配字符打印===="
sed -n '/^work.*whois$/p' 6.example.txt  #匹配打印以work开头、whois结尾的行
echo "==============="
sed -n '/Apple Banana/,/worker boy/{/^like.*/p}' 6.example.txt  #匹配Apple Banana这行与worker boy这行之间,包含like开头的行
echo "==============="
sed -i '/Cherry/{s#Banana#Summer#}' 6.example.txt #匹配Cherry这行并替换Banana为Summer
cat 6.example.txt

#4.在指定行前或行后插入一行
echo "============在指定行前或行后插入一行===="
sed -i '2 iI Love You' 6.example.txt            # 在第2行前插入I Love You
cat 6.example.txt
echo "==============="
sed -i '/^I.*You$/iLinux Shell'  6.example.txt  #在符合匹配条件行前面插入一行i后边的文字Linux Shell
cat 6.example.txt
echo "==============="
sed -i '/^I.*You$/aGood Morning Linux'  6.example.txt  #在符合匹配条件行后插入一行i后边的文字Good Morning Linux
cat 6.example.txt
echo "==============="
sed -i '4 aLinux Is Good System' 6.example.txt  #在第4行后插入Linux Is Good System
cat 6.example.txt


#5.删除匹配条件的上下行
#echo "============删除匹配条件的行===="
sed -i -e '/sdasda/{n;d}' -e '$!N;/\n.*sdasda/!P;D' 6.example.txt    #删除匹配行的上一行和下一行
cat 6.example.txt
AA="sdasda"
sed -i -e '/'"$AA"'$/{n;d}' -e '$!N;/\n.*'"$AA"'$/!P;D' 6.example.txt  #sed中使用变量,删除匹配行的上一行和下一行:
cat 6.example.txt

#5.根据匹配条件再进行替换
echo "============匹配条件再进行替换===="
#匹配ForceCommand cvs server和AllowTcpForwarding no之间,匹配到http的行的下一行再进行<td>替换成逗号
cat 6.example.txt |sed -n '/ForceCommand cvs server/,/AllowTcpForwarding no/{/http/{n;s#<td>#,#gp}}'

echo "==============="
#表示匹配ForceCommand cvs server和AllowTcpForwarding no之间,然后匹配到<td>[0-9] 的行然后将<td> 替换为空并打印
cat 6.example.txt |sed -n '/ForceCommand cvs server/,/AllowTcpForwarding no/{/<td>[0-9]/s#<td>##p}'

echo "==============="
#表示将<td>及前面多有字符都替换为空
cat 6.example.txt |sed -n '/ForceCommand cvs server/,/AllowTcpForwarding no/{/<td>[0-9]/s#.*<td>##p}'

三、grep的用法

  -E :正则表达式匹配
  -i :忽略大小写
  -v :反过来,只打印没有匹配的,而匹配的反而不打印。
  -n :显示行号
  -w :被匹配的文本只能是单词,而不是单词中的某一部分,如搜寻单词work时,若文本中有worker是不会显示的。
  -c :显示总共有多少行被匹配到了,而-cv选项是显示有多少行没有被匹配到。
  -o :只显示被模式匹配到的字符串。
  --color :将匹配到的内容以颜色高亮显示。
  -A n:显示匹配内容所在行及其后n行,after
  -B n:显示匹配内容所在行及其前n行,before
  -C n:显示匹配内容所在行及其前后各n行,context
#egrep == grep -E
#zgrep == zcat *.gz|grep 
echo "============grep的使用方法如下===="
#实列:
grep -E "Apple[0-9]|work" 6.example2.txt  #正则匹配6.example2.txt中的work和Apple1989的行
echo "==============="
grep -i "apple" 6.example2.txt #忽略大小写匹配6.example2.txt中apple的行
echo "==============="
grep -v "apple" 6.example2.txt  #排除6.example2.txt中apple的行
echo "==============="
grep -n "apple" 6.example2.txt  #过滤6.example2.txt中apple的行并显示行号
echo "==============="
grep "work" 6.example2.txt  #过滤6.example2.txt中work的行
echo "==============="
grep -w "work" 6.example2.txt  #过滤6.example2.txt中包含work单词的行
echo "==============="
grep -c "work" 6.example2.txt #统计6.example2.txt中包含work字符串有多少行
echo "==============="
grep -o "work" 6.example2.txt #过滤6.example2.txt中包含work行并只打印work字符串
echo "==============="
grep --color "work" 6.example2.txt #过滤6.example2.txt中包含work字符串并高亮显示
echo "==============="
grep -A 2 "apple" 6.example2.txt #过滤6.example2.txt中apple开始及后两行
echo "==============="
grep -B 2 "apple" 6.example2.txt #过滤6.example2.txt中apple开始及前两行
echo "==============="
grep -C 1 "apple" 6.example2.txt #过滤6.example2.txt中apple开始及前后一行
echo "==============="

四、课程附件

附件1:6.example.txt

one two three
Apple Banana Cherry
worker When People
Grape Mango Orange

work love you whois

like what when where
liker how many name

Unix system is Good
worker boy girl people
onetwothreefourfivesix
#TCPKeepAlive yes
UseLogin no
maoge232342213



ForceCommand cvs server
http Is HyperText Transfer Protocol
Shell<td>Is<td>Good
This number:<td>10086<td>is Yidong
This number:<td>10086 is good Number
AllowTcpForwarding no

附件2:6.example2.txt内容:

Apple
Apple1989
apple
harding work
he is worker
相关文章
|
19天前
|
存储 运维 Shell
shell中for while until 三种循环的用法
shell编程中,有几种常见的循环结构,包括for循环、while循环和until循环,总的来说,循环shell编程中扮演着至关重要的角色,它们使得自动化任务变得更加容易,提高了效率,并且可以处理各种各样的编程需求。
262 13
shell中for while until 三种循环的用法
|
19天前
|
Shell 数据安全/隐私保护
shell中的通配符 熟悉grep、cut、sort等小工具和shell中的通配符的使用
shell中的通配符 熟悉grep、cut、sort等小工具和shell中的通配符的使用
37 0
|
8月前
|
存储 Shell Linux
Shell 编程:探索 Shell 的基本概念与用法
Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。 Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。 Shell 脚本(shell script),是一种为 shell 编写的脚本程序,shell 和 shell script 是两个不同的概念。
89 0
|
8月前
|
监控 Shell Linux
Linux Shell高级用法:优化和自动化你的工作流程
Linux Shell是一个非常强大的工具,可以用于自动化任务、处理文本和数据、进行系统管理等。在这篇文章中,我们将介绍一些Linux Shell的高级用法,帮助你更高效地利用Shell完成各种任务。
109 0
|
19天前
|
运维 Shell Python
第七章 Shell文本处理三剑客之grep
第七章 Shell文本处理三剑客之grep
|
19天前
|
Shell Perl
shell学习(九) 【shell sed用法】
shell学习(九) 【shell sed用法】
15 0
|
19天前
|
Shell C语言 C++
【Shell 编程指南】shell中的(),{}几种语法用法
【Shell 编程指南】shell中的(),{}几种语法用法
21 0
|
19天前
|
算法 Shell Linux
【Shell 命令集合 文档编辑】Linux 文本搜索工具 grep命令使用指南
【Shell 命令集合 文档编辑】Linux 文本搜索工具 grep命令使用指南
34 4
|
19天前
|
Shell
shell脚本for循环复杂用法
shell脚本for循环复杂用法
51 5
|
19天前
|
存储 Shell Linux
Linux的shell命令——变量用法
Linux的shell命令——变量用法
34 0