grep只能显示匹配搜索模式的单词吗?是否有办法使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使用相同的搜索,我希望它输出的是:the
the
the
this
thoroughly这有可能使用grep吗?还是使用另一种组合的工具?
3 回答
萧十郎
TA贡献1815条经验 获得超13个赞
试试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.
PIPIONE
TA贡献1829条经验 获得超9个赞
awk
# awk '{for(i=1;i<=NF;i++){if($i~/^th/){print $i}}}' file the the the this thoroughly
添加回答
举报
0/150
提交
取消