不遵循StackOverflow最佳实践的道歉
我想要实现的是假设
FILE1.TXT
something blah blah
something blah blah target_pattern blah blah target_pattern
FILE2.TXT
something blah blah
something blah blah target_pattern blah blah
file3.txt
something blah blah
something blah blah target_pattern blah blah target_pattern blah blah target_pattern
我希望输出为
file1.txt target_pattern blah blah target_pattern
file3.txt target_pattern blah blah target_pattern blah blah target_pattern
注意file2.txt有target_pattern但没有重复。 如果你知道如何计算" target_pattern"每个grep搜索结果的单词,也适用于我。
例如字数:
file1.txt 2
file2.txt 1
file3.txt 3
到目前为止我尝试过的是:
> cat q.txt
something blah blah
something blah blah target_pattern blah blah target_pattern
> cat w.txt
something blah blah
something blah blah target_pattern blah blah
>cat e.txt
something blah blah
something blah blah target_pattern blah blah target_pattern
> grep -r "target_pattern" ./*
./e.txt:something blah blah target_pattern blah blah target_pattern
./q.txt:something blah blah target_pattern blah blah target_pattern
./w.txt:something blah blah target_pattern blah blah
> grep -r "target_pattern" ./*|grep "target_pattern"
./e.txt:something blah blah target_pattern blah blah target_pattern
./q.txt:something blah blah target_pattern blah blah target_pattern
./w.txt:something blah blah target_pattern blah blah
> grep -r "target_pattern" ./*| wc -w
20
> grep -r "target_pattern" ./*| wc -l
3
答案 0 :(得分:0)
Grep for 2次target_pattern,中间有一些东西(。*):
grep -E "target_pattern.*target_pattern" targetpat.txt
something blah blah target_pattern blah blah target_pattern
something blah blah target_pattern blah blah target_pattern blah blah target_pattern
了解模式匹配/正则表达式以了解更多信息。