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
提交
取消