加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

SHELL实战day4

发布时间:2020-12-15 23:07:30 所属栏目:安全 来源:网络整理
导读:一 正则介绍_grep 1:grep用来过滤关键词 grep [-cinvABC] ‘word‘ filename -c 行数 (count) -i 不区分大小写 -n 显示行号 -v 取反 -r 遍历所有子目录 -A 后面跟数字,过滤出符合要求的行以及下面n行 -B 同上,过滤出符合要求的行以及上面n行 -C 同上,同
一 正则介绍_grep

1:grep用来过滤关键词
grep [-cinvABC] ‘word‘ filename
-c 行数 (count)
-i 不区分大小写
-n 显示行号
-v 取反
-r 遍历所有子目录


-A 后面跟数字,过滤出符合要求的行以及下面n行
-B 同上,过滤出符合要求的行以及上面n行
-C 同上,同时过滤出符合要求的行以及上下各n行
2:示例
grep -n ‘root‘ /etc/passwd
grep -nv ‘nologin‘ /etc/passwd
grep ‘[0-9]‘/etc/inittab
grep -v ‘[0-9]‘/etc/inittab
grep -v ‘^#‘ /etc/inittab
grep -v ‘^#‘ /etc/inittab|grep -v ‘^$‘
grep ‘^[^a-zA-Z]‘ test.txt
grep ‘r.o‘ test.txt
.表示任意一个字符
grep ‘oo‘ test.txt
表示左边的字符重复0到n次
grep ‘.‘ test.txt
.
表示任意字符
grep ‘o{2}‘ /etc/passwd
{}表示前面字符的重复范围
egrep ‘o{2}‘ /etc/passwd
和上面一样的效果
egrep ‘o+‘ /etc/passwd
+符号前面字符的一次或多次
egrep ‘oo?‘ /etc/passwd
?表示前面字符重复0或1次
egrep ‘root|nologin‘ /etc/passwd
|表示或者的意思
egrep ‘(oo){2}‘ /etc/passwd

二 sed

1: sed -n ‘5‘p test.txt
sed -n ‘1,5‘p test.txt
sed -n ‘1,$‘p test.txt
sed -n ‘/root/‘p test.txt
sed -n ‘/root/‘Ip test.txt
匹配大小写


sed -n ‘/^1/‘p test.txt
sed -n ‘in$‘p test.txt
sed -n ‘/r..o/‘p test.txt
sed -n ‘oo*‘p test.txt
sed -e ‘1‘p -e ‘/111/‘p -n test.txt

2: sed ‘1‘d test.txt
sed ‘1,3‘d test.txt
sed -i ‘1,3‘d test.txt
删除文件内容


sed ‘/oot/‘d test.txt
sed ‘1,2s/ot/to/g‘ test.txt
sed ‘s#ot#to#g‘ test.txt
sed ‘s/[0-9]//g‘ test.txt
sed ‘s/[a-zA-Z]//g‘ test.txt
sed -r ‘s/(rot)(.)(bash)/321/‘ test.txt

sed ‘s/^.
$/123&/‘ test.txtsed -i ‘s/ot/to/g‘ test.txt

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读