繁体   English   中英

如何在word2 -bash所在的行之后计算没有单词的行

[英]How to count lines without word after line where is word2 -bash

您好我有与链接有关的问题

我有一个很大的文本文件,其中某些行包含单词"DataMeetingIs11" ,而下一行包含单词"done"或note。 我的任务是计算所有此类行。 例如,我要计算没有“完成”字样的行

grep -A 1 DataMeetingIs11 your_file|fgrep -v -c

没用

您需要添加-P Perl-regexp参数来实现以上目的。

grep -oPz '.*DataMeetingIs11.*(?=\n.*done)' file | wc -l

如有必要,添加单词边界。

grep -oPz '.*\bDataMeetingIs11\b.*(?=\n.*\bdone\b)' file | wc -l

例:

$ cat fa
 DataMeetingIs11
done
foo
bar
 DataMeetingIs11
not 
 DataMeetingIs11
fio done
$ grep -oPz '.*DataMeetingIs11.*(?=\n.*done)' fa | wc -l
2

通过使用awk脚本,您还可以执行以下操作:

#!/usr/bin/awk -f

$0 ~ /DataMeetingIs11/ {
    line=$0
    next
}
$0 ~ /done/ && line != "" {
    count++
}
{
    line = ""
}
END {
    print count
}

例:

$ cat file
blabla
 DataMeetingIs11
done
something
 DataMeetingIs11
alt
 DataMeetingIs11
it's done

该文件的执行结果:

$ ./DataMeetingIs11.awk file
2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM