二、文本处理工具(三剑客grep、sed、awk)

一、grep

  • grep支持正则表达式
  • egrep支持扩展的正则表达式
  • grep -E = egrep

1.grep格式

  • grep 匹配条件 处理文件
grep -E "bash|nologin" passwd
grep -E root passwd #过滤root关键字
grep -Ei root passwd #忽略大小写
grep -Ei "\<root" passwd #root字符之前不能有字符
grep -Ei "root\>" passw #root字符之后不能有字符
grep -3 root passwd #显示过滤行以及上面几行和下面几行
grep -nEi "root\>" passwd #显示匹配的行所在行号
grep -A3 root passwd #显示过滤行以及下面几行
grep -B3 root passwd #显示过滤行以及上面几行
grep -v root passwd #反向过滤

问题:复制/etc/passwd文件到目录中,抓取不是以root开头和结尾的含有root的文件(练习题专栏!)

2.grep字符数量匹配规则

~~
^westos以westos开头
westos$以westos结尾
w…sw开头s结尾中间4个任意字符
…ss结尾前面5个任意字符
*字符出现任意
?0到1次
+1次到任意次
{n}n次
{m,n}m到n次
{0,n}0-n次
{,n}0-n次
{m,}最少m次
(lee){2}lee字符串出现2次
grep w...s sss
grep w.*s sss
grep -E 'w.?s' sss
grep -E 'w.+s' sss
grep -E 'w.{4}s' sss
grep -E 'w.{2,4}s' sss
grep -E 'w.{,4}s' sss
grep -E 'w.{4,}s' sss
grep -E 'w(es){,2}s' sss

练习脚本:
请显示系统中能被su命令切换的用户名称(练习题专栏!!!)

二、sed

1.命令格式

sed 参数 命令 处理对象
sed 参数 处理对象 -f 处理规则文件

2.对字符的处理

(1)p-----显示
-n 静默输出

sed -n 5p sedfile #显示第五行
sed -n 3,5p sedfile #显示3到5行
sed -n "3p;5p" sedfile #显示3和5行
sed -ne '5,$p' sedfile #5到最后一行
sed -n '/^mail/p' sedfile #显示以mail开头的行

(2)d-----删除
不加-n原因:处理行被删掉,默认行不显示,也就什么都没有了

sed 5d sedfile #删除第五行
sed '/^root/d' sedfile #把root开头的行删除
sed '/^root/!d' sedfile #除了以root开头的行都删除
sed '5d;$d' sedfile #删除第五行和第十行

(3)a-----添加
添加是添加在下方

sed '5a hello' sedfile #添加到第五行
sed '/root/a hello' sedfile #添加到含有root的行
sed '$a hello' sedfile #添加到最后一行
sed '$a hello\njjr' sedfile #添加的内容换行

(4)c-----替换

sed '1c hello jjr' sedfile #第一行替换成hello jjr
sed '/root/c hello jjr' sedfile #含有root的行替换

(5)w-----写入

sed '/root/w www' sedfile #把符合的行写到指定文件中
cat www

(6)i-----插入
插入是插入在上方,和a的用法类似

sed '1i hello\njjr' sedfile

(7)r-----整合文件
-i 处理过后的结果保存到文件当中

cat file1 file2 > file
cat file2 file1 > file
cat file1 >> file2 #将file1整合到file2后面
cat file2 >> file1 #将file2整合到file1后面
sed '5r sedfile' www #将sedfile的第五行整合到www中
sed '5r sedfile' -i www #处理过后的结果永久保存到文件当中

3.字符替换

sed '=' passwd #每一行前面加行号
sed '=' passwd | sed 'N;s/\n/ /g' #替换换行,N提前加载处理下一行
#(sed是逐行处理,在处理上一行的时候还不知道下一行的存在)
sed '$G' jjr #给最后一行换行
sed '$!G' jjr #除了最后一行都换行
sed 's/:/#/g' passwd #替换全文;s全文,g每行的全部
sed 's/:/#/' passwd #每行的第一个
sed '1s/:/%%%/g' testfile #第一行换
sed '1,5s/:/%%%/g' testfile #1到5行换
sed '1s/:/%%%/g;5s/:/%%%/g' testfile #第一行和第五行换
sed '/mail/,/ftp/s/:/%%%/g' testfile #指定字符之间换
sed 's/\//%%%/g' testfile #全文的/替换;“\”为转义字符
sed 's@/@%%%@g' testfile #也可以将/用@
sed 's/:/#/g' -i passwd

练习脚本:
Apache_port.sh,此脚本接入数字,http的端口就改为此数字,假设selinux为关闭状态
例如:
sh Apache_port.sh
ERROR: Pleaase input port number following script !!
sh Apache_port.sh 8080
apache的端口会被修改为8080
(练习题专栏!!!)

三、awk

awk -F 分隔符 BEGIN{}{}END{} FILENAME

NR行数
NF列数
FILENAME文件名称本身
westoswestos变量值
“westos”westos字符串
/bash$/条件
/条件1条件2/
/条件1/||/条件2/条件1或者条件2
/条件1/&&/条件2/条件1并且条件2
$0所有的列
$1第一列
$2第二列
$3第三列
awk -F : 'BEGIN{print begin}{print $1}END{print end}' testfile #起始结尾为变量值
awk -F : 'BEGIN{print "begin"}{print $1}END{print "end"}' testfile #起始结尾为字符串
awk -F : 'BEGIN{N=0}{N++}END{print N}' testfile #统计行数
awk -F : '{print $1}' testfile #抽取第一列
awk -F : '/bash$/{print $0}' testfile #抽取以bash结尾的所有列
awk -F : '/bash$/&&/root/{print $0}' testfile #抽取以bash结尾并且含有root关键字的所有列
awk -F : '/bash$/&&!/root/{print $0}' testfile #抽取以bash结尾并且不含有root关键字的所有列
awk -F : '/bash$/||/mail/{print $0}' testfile #抽取以bash结尾或者含有mail关键字的所有列
awk -F : '/bash$|mail/{print $0}' testfile #抽取以bash结尾或者含有mail关键字的所有列
awk -F : '!/bash$|mail/{print $0}' testfile #不是以bash结尾并且不含有mail关键字的所有列
awk -F : '$7~/bash/{print $1}' testfile #抽取第七列是bash关键字的第一列
awk -F : '$7!~/bash/{print $1}' testfile #抽取第七列不是bash关键字的第一列

课后练习:
1.统计在系统中能su切换的并且用户加目录不在/home下的用户数量
2.ps ax -o %mem 统计内存的总共使用量(即全部相加)
(练习题专栏!!!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贾几人要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值