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

时间:2016-11-17 05:57:33

标签: unix count words

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

tom is really really cool!  joe for the win!
tom is actually smart.
tom and joe are best buddies. Joe is smart.
joe works hard

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

1 个答案:

答案 0 :(得分:1)

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

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