ubuntu中文本出现的grep

ubuntu中文本出现的grep,grep,Grep,我目前有一个文本文件,其中包含以下几行文本。我需要使用grep来显示不同场景下文件中出现的单词test的行数。下面是我尝试过的一些东西,但输出是错误的。我对使用grep还很陌生,所以希望有人能在我做错的地方提供帮助 test.txt: this is some testing for my own test purpose. testing testing testing 1 2 3 test test-case 1 is for delete purpose tester’s is co

我目前有一个文本文件,其中包含以下几行文本。我需要使用grep来显示不同场景下文件中出现的单词
test
的行数。下面是我尝试过的一些东西,但输出是错误的。我对使用grep还很陌生,所以希望有人能在我做错的地方提供帮助

test.txt:

this is some testing for my own test purpose.

testing testing testing 1 2 3 test

test-case 1 is for delete purpose

tester’s is coming to test out the new devices

Tester test 3 test case
场景1:

this is some testing for my own test purpose.

testing testing testing 1 2 3 test

test-case 1 is for delete purpose

tester’s is coming to test out the new devices

Tester test 3 test case
报告文本文件测试中有多少行包含区分大小写的单词“test”。它不应该包括作为另一个单词的子字符串的单词计数。例如测试、测试人员、测试用例

回答:
grep“\btest\b”测试| wc-l

场景2:

this is some testing for my own test purpose.

testing testing testing 1 2 3 test

test-case 1 is for delete purpose

tester’s is coming to test out the new devices

Tester test 3 test case
报告区分大小写的单词“test”在文本文件test中出现的次数(在其他单词中为ok)

回答:
grep–o“测试”测试| wc-w

场景3:

this is some testing for my own test purpose.

testing testing testing 1 2 3 test

test-case 1 is for delete purpose

tester’s is coming to test out the new devices

Tester test 3 test case
报告“test”一词在文本文件test的最后两行中出现的次数


答案:
tail-n2测试| grep-o“\btest\b”| wc-w

您可以尝试以下方法:

情景1:

$ grep -i -c "[^a-z]test[^a-z]" test.txt
情景2:

$ grep –o “test” test.txt | wc -l
情景3:

$ tail -2 test.txt | grep -o "[^a-z]test[^a-z]" | wc -l