如何计算行之后与字线在哪里字词2 -bash [英] How to count lines with word after line where is word2 -bash
本文介绍了如何计算行之后与字线在哪里字词2 -bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大的文本文件,其中一些行包含单词DataMeetingIs11
其中,以下行包含单词完成
。我的任务是计算所有这样的行。比如我想算如下:
I have a big text file where some lines contain the word "DataMeetingIs11"
where the following line contains the word "done"
. My task is to count all such lines. For example I want to count the following:
......DataMeetingIs11.....
....done..................
但不是以下内容:
but not the following:
......DataMeetingIs11.....
..........................
我尝试使用下一个命令:
I tried using the next command:
grep -A 1 DataMeetingIs11 file| grep -c done
但它没有工作。你能帮帮我吗?
but it didn't work. Can you please help me?
修改
如何计算不做字线?
推荐答案
您可以用awk来代替:
You can use awk instead:
awk '/DataMeetingIs11/ {a++; p=NR} /done/ && NR==(p+1) {c++}
END{print "Without done:", (a-c) ", With done:", c}' file
Without done: 1, With done: 2
说明:
/DataMeetingIs11/ # when input line matches literal "DataMeetingIs11"
{a++; p=NR} # store current line # NR into variable p and increment a
/done/ && NR==(p+1) # when line matches "done" and when
# current line # is p+1 (next line)
{c++} # increment a counter c
END{print (a-c), c} # print counts the end
这篇关于如何计算行之后与字线在哪里字词2 -bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文