第一种方法
cat -n pom.xml | grep abc
上面的命令是打开pom.xml文件,并显示行号,查找关键字abc,这个-n就是显示pom.xml这个文件的行号
从上面的图片可以看到在cat -n pom.xml的时候会显示出行号。
我们再用cat -n pom.xml | grep com.bj58.qdyw.huangye.core ,显示出com.bj58.qdyw.huangye.core这个关键字所在的行在16行
我现在要查看com.bj58.qdyw.huangye.core这个关键字的前三行和后三行,用下面的命令
cat -n pom.xml | tail -n +13 | head -n 6
tail -n +13意思是从文件的第13行往后显示,head -n 6 的意思是显示13行后的6行
总结:
先求得关键字的行号,比如求得关键字的行号是100行
cat -n filename | grep 关键字
查看关键字的前后50行
cat -n filename |tail -n +50 | head -n 100
第二种方式:
cat filename |grep 关键字 -C10 上面显示关键字的前后10行 -C显示前后多少行
cat filename |grep 关键字 -A10 上面显示关键字的后10行 -A显示后多少行
cat filename |grep 关键字 -B10 上面显示关键字的前10行 -B显示前多少行
cat -n 2019-01-25file |grep b103303 -C2