美文网首页
【linux】正则表达式

【linux】正则表达式

作者: 鸦言 | 来源:发表于2020-07-15 19:39 被阅读0次

grep\sed\awk 都是针对文本行的操作

grep、egrap

grep [-cinvABC] ‘word’ filename
-c :打印符合要求的行数
-i :忽略大小写
-n :在输出符合要求的行的同时连同行号一起输出
-v :打印不符合要求的行
-A :后跟一个数字(有无空格都可以),例如 –A2则表示打印符合要求的行以及下面两行
-B :后跟一个数字,例如 –B2 则表示打印符合要求的行以及上面两行
-C :后跟一个数字,例如 –C2 则表示打印符合要求的行以及上下各两行

下面例子用到的原文档:

cat 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

例子1:

grep -A 2 3 123.txt
hello3 world3
hello world
hello world
--
hello3 world3
hello2 world2

例子2:过滤出带有某个关键词的行并输出行号

grep -n "3" 123.txt

4:hello3 world3
8:hello3 world3

例子3: 过滤不带有某个关键词的行,并输出行号

grep -vn '3' 123.txt

1:hello2 world2
2:hello world
3:hello world
5:hello world
6:hello world
7:hello world
9:hello2 world2

例子4:过滤出所有包含数字的行

grep [0-9] 123.txt

hello2 world2
hello3 world3
hello3 world3
hello2 world2

例子5:过滤出文档中以某个字符开头或者以某个字符结尾的行
"^"表示行的开始

grep "^h" 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

"$"表示行的结束

grep "3$" 123.txt

hello3 world3
hello3 world3

"^$"表示空行(文档中没空行,无输出)

grep "^$" 123.txt
 

打印出不以英文字母开头的行
第一个 "^" 表示开头,第二个 "^" 表示否定。

grep "^[^a-zA-Z]" 123.txt

grep "^[a-zA-Z]" 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

过滤任意一个字符与重复字符
“.”表示任意一个字符
“*”表示零个或多个前面的字符

grep "o*" 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

指定要过滤字符出现的次数

这里用到了{ },其内部为数字,表示前面的字符要重复的次数。{}左右都需要加上脱意字符’\’。另外,使用{ }我们还可以表示一个范围的,具体格式是 ‘\’其中n1<n2,表示重复n1到n2次前面的字符,n2还可以为空,则表示大于等于n1次。

grep "l\{2\}" 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2
image.png

上面部分讲的grep,另外笔者常常用到egrep这个工具,简单点讲,后者是前者的扩展版本,我们可以用egrep
完成grep不能完成的工作,当然了grep能完成的egrep完全可以完成。

sed 工具的使用

sed工具以及下面要讲的awk工具就能实现把替换的文本输出到屏幕上的功能了,而且还有其他更丰富的功能。sed和awk都是流式编辑器,是针对文档的行来操作的。

例子1:打印某行 e sed -n ‘n’p filename 单引号内的n n 是一个数字,表示第几行

sed -n "2"p 123.txt

hello world

例子2:打印多行 打印整个文档用 -n ‘1,$’p

sed -n "2,4"p 123.txt

hello world
hello world
hello3 world3
sed -n "1,$"p 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

例子3:打印包含某个字符串的行

sed -n "/o3/"p 123.txt

hello3 world3
hello3 world3

例子4:上面grep中使用的特殊字符,如’^’, ‘$’, ‘.’, ‘*’等同样也能在sed中使用。

sed -n "/3$/"p 123.txt

hello3 world3
hello3 world3

例子5: -e 可以实现多个行为

sed -e "/2/"p  -e "/3/"p -n 123.txt

hello2 world2
hello3 world3
hello3 world3
hello2 world2

例子7: 删除某行或者多行(d 就是删除选项)

sed -n "/2/"p 123.txt 

hello2 world2
hello2 world2

sed "/2/"d 123.txt 

hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3

例子8: 替换字符或字符串

’s’就是替换的命令,’g’为本行中全局替换,如果不加’g’,只换该行中出现的第一个。除了可以使用’/’外,还可以使用其他特殊字符例如’#’或者’@’都没有问题。

sed "1,3s/ll/zz/g" 123.txt

hezzo2 world2
hezzo world
hezzo world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

sed "1,3s/z/l/g" 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

例子9:删除文档中的所有数字或者字母

sed "s/[0-9]//g" 123.txt

hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

例子10:调换两个字符串的位置

命令的大概意思是,把原行分成3段,分别赋予新位置。

用’()’把所想要替换的字符括起来成为一个整体,因为括号在sed中属于特殊符号,所以需要在前面加脱意字符’\’,替换时则写成’\1’, ‘\2’, ‘\3’ 的形式。除了调换两个字符串的位置外,笔者还常常用到在某一行前或者后增加指定内容。

sed "s/\(hello\)\(.*\)\(world\)/\3\2\1/" 123.txt

world2 hello2
world hello
world hello
world3 hello3
world hello
world hello
world hello
world3 hello3
world2 hello2

例子11:直接修改文件的内容

sed -i ‘s/:/#/g’ test.txt ,这样就可以直接更改test.txt文件中的内容了。由于这个命令可以直接把文件修
改,所以在修改前最好先复制一下文件以免改错。

cat 123.txt

hello2 world2
hello world
hello world
hello3 world3
hello world
hello world
hello world
hello3 world3
hello2 world2

sed -i "s/\(hello\)\(.*\)\(world\)/\3\2\1/" 123.txt

cat 123.txt

world2 hello2
world hello
world hello
world3 hello3
world hello
world hello
world hello
world3 hello3
world2 hello2

awk 工具的使用

awk比sed更加强大,它能做到sed能做到的,同样也能做到sed不能做到的。awk工具其实是很复杂的,有专门的书籍来介绍它的应用。

例子1:截取文档中的某个段

cat 123.txt |awk -F'h' '{print $1}'

world2 
world 
world 
world3 
world 
world 
world 
world3 
world2 

cat 123.txt |awk -F'h' '{print $2}'

ello2
ello
ello
ello3
ello
ello
ello
ello3
ello2

例子2:匹配字符或字符串

awk '/hello3/' 123.txt

world3 hello3
world3 hello3

相关文章

网友评论

      本文标题:【linux】正则表达式

      本文链接:https://www.haomeiwen.com/subject/uosbcktx.html