作为运维人员,需要学会文件内容查找功能,特别是一些配置文件等。
之前一般使用vi命令打开一个文件,然后使用按/输入关键字进行查找,按:set nu
可以显示行号。
在学习过程中,学到了另外一种方法。
sed 的等号可以显示行号,用p参数可以进行匹配打印输出。
[root@localhost shell]# cat helloworld.txt
hello world
abcd
huangbaokang
[root@localhost shell]# sed -n '/bao/{=;p}' helloworld.txt
3
huangbaokang
前面/bao/
是匹配表达式,因为要执行两条命令,所以使用{}
,两条命令使用分号隔开。