Linux 一些文本处理指令的学习
重点只放出了自己没怎么用过的进行学习记录
grep -o 匹配输出
实例引用
只是匹配输出的意义不大,但我经过搜索,发现在一些特定场景中可以进一步简化操作,直接提取出目标值。
例如对html中某一个属性值的提取,常用来爬虫。
下面展示一些 内联代码片
。
...
<input type="hidden" name="fd" value="Q2m4nLJN4aGSAYmtrsIZAw6vnQRy" />
<input type="hidden" name="execution" value="e1s1" />
<input type="hidden" name="_eventId" value="submit" />
...
任务是提取出name为execution的value值,常规的匹配做法:
[root@localhost grep-o]# cat page.html | grep 'name="execution" value="[a-zA-Z0-9-]\+".*>'
<input type="hidden" name="execution" value="e1s1" />
而使用grep -o可以更灵活的只输出匹配字段:
[root@localhost grep-o]# cat page.html | grep 'name="execution" value="[a-zA-Z0-9-]\+".*>' | grep -o 'e[0-9][0-9a-zA-Z]\+'
e1s1
grep -q 对于匹配判断
对于模式是否匹配的判断,常用于if判断语句中,q只安静模式,本身无输出。
└─[$] <> if grep -q hello test ; then echo yes;else echo no; fi
no
└─[$] <> if grep -q aa test ; then echo yes;else echo no; fi
yes