有没有一种方法能够使grep输出从与搜索表达式匹配的文件中输出的“单词”? 正则表达式
若是我想在多个文件中找到“ th”的全部实例,则能够执行如下操做: 工具
grep "th" *
可是输出将是这样的(我大胆); 测试
some-text-file : the cat sat on the mat some-other-text-file : the quick brown fox yet-another-text-file : i hope this explains it thoroughly
使用相同的搜索,我但愿它输出的是: ui
the the the this thoroughly
使用grep能够吗? 仍是使用其余工具组合? this
$ grep -w
摘自grep手册页: spa
-w:仅选择那些包含组成整个单词的匹配项的行。 测试是匹配的子字符串必须在该行的开头,或者必须在非单词组成字符以前。 code
grep命令仅用于匹配和perl 文档
grep -o -P 'th.*? ' filename
我有一个相似的问题,正在寻找grep / pattern regex和“找到匹配的模式”做为输出。 字符串
最后,我使用了egrep(在grep -e或-G上的正则表达式没有给我相同的egrep结果)和-o选项 get
因此,我认为这可能相似于(我不是正则表达式大师):
egrep -o "the*|this{1}|thoroughly{1}" filename
awk
,无需工具组合。
# awk '{for(i=1;i<=NF;i++){if($i~/^th/){print $i}}}' file the the the this thoroughly
试试grep -o
grep -oh "\w*th\w*" *
编辑:从菲尔的评论匹配
从文档 :
-h, --no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search. -o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.