简体   繁体   中英

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

Hello i have question related to link

I have a big text file where some lines contain the word "DataMeetingIs11" where the following line contains the word "done" or note . My task is to count all such lines. For example I want to count this lines without "done " word

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

didn't work

You need to add -P Perl-regexp parameter to achieve the above.

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

Add word boundaries if necessary.

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

Example:

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

By using a awk script you can also do this:

#!/usr/bin/awk -f

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

Example:

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

Execution result on this file:

$ ./DataMeetingIs11.awk file
2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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