如何在UNIX中计算一行中随机出现的2个单词

如何在UNIX中计算一行中随机出现的2个单词,unix,count,words,Unix,Count,Words,示例(文件包含以下文本): 现在,我想在这个文件中找不到joe和tom使用UNIX在单行中使用的行。 因此答案应该是2(即仅第一行和第四行)您可以使用带有-c标志的普通grep来计算实例。对于您的输入文件 grep 'tom.*joe\|joe.*tom' file tom is really really cool! joe for the win! tom and joe are best buddies. Joe is smart. grep -c 'tom.*joe\|joe.*to

示例(文件包含以下文本):

现在,我想在这个文件中找不到joe和tom使用
UNIX
在单行中使用的行。
因此答案应该是2(即仅第一行和第四行)

您可以使用带有
-c
标志的普通
grep
来计算实例。对于您的输入
文件

grep 'tom.*joe\|joe.*tom' file
tom is really really cool!  joe for the win!
tom and joe are best buddies. Joe is smart.

grep -c 'tom.*joe\|joe.*tom' file
2

我在写这么长的程序(grep是如此强大!
joe
tom
在一行中使用,在第1行和第3行中使用,而不是第4行中使用。因为您在这里非常新,所以答案很简单:构造DFA(这正是grep在内部所做的)在您的例子中,您甚至可以手工构建它,只有两个模式共享一个字母,并且它们不重叠。
grep 'tom.*joe\|joe.*tom' file
tom is really really cool!  joe for the win!
tom and joe are best buddies. Joe is smart.

grep -c 'tom.*joe\|joe.*tom' file
2