Saturday, 12 August 2017

bash - Check if all of multiple strings or regexes exist in a file



I want to check if all of my strings exist in a text file. They could exist on the same line or on different lines. And partial matches should be OK. Like this:



...
string1
...

string2
...
string3
...
string1 string2
...
string1 string2 string3
...
string3 string1 string2
...

string2 string3
... and so on


In the above example, we could have regexes in place of strings.



For example, the following code checks if any of my strings exists in the file:



if grep -EFq "string1|string2|string3" file; then
# there is at least one match

fi


How to check if all of them exist? Since we are just interested in the presence of all matches, we should stop reading the file as soon all strings are matched.



Is it possible to do it without having to invoke grep multiple times (which won't scale when input file is large or if we have a large number of strings to match) or use a tool like awk or python?



Also, is there a solution for strings that can easily be extended for regexes?


Answer



Awk is the tool that the guys who invented grep, shell, etc. invented to do general text manipulation jobs like this so not sure why you'd want to try to avoid it.




In case brevity is what you're looking for, here's the GNU awk one-liner to do just what you asked for:



awk 'NR==FNR{a[$0];next} {for(s in a) if(!index($0,s)) exit 1}' strings RS='^$' file


And here's a bunch of other information and options:



Assuming you're really looking for strings, it'd be:




awk -v strings='string1 string2 string3' '
BEGIN {
numStrings = split(strings,tmp)
for (i in tmp) strs[tmp[i]]
}
numStrings == 0 { exit }
{
for (str in strs) {
if ( index($0,str) ) {
delete strs[str]

numStrings--
}
}
}
END { exit (numStrings ? 1 : 0) }
' file


the above will stop reading the file as soon as all strings have matched.




If you were looking for regexps instead of strings then with GNU awk for multi-char RS and retention of $0 in the END section you could do:



awk -v RS='^$' 'END{exit !(/regexp1/ && /regexp2/ && /regexp3/)}' file


Actually, even if it were strings you could do:



awk -v RS='^$' 'END{exit !(index($0,"string1") && index($0,"string2") && index($0,"string3"))}' file



The main issue with the above 2 GNU awk solutions is that, like @anubhava's GNU grep -P solution, the whole file has to be read into memory at one time whereas with the first awk script above, it'll work in any awk in any shell on any UNIX box and only stores one line of input at a time.



I see you've added a comment under your question to say you could have several thousand "patterns". Assuming you mean "strings" then instead of passing them as arguments to the script you could read them from a file, e.g. with GNU awk for multi-char RS and a file with one search string per line:



awk '
NR==FNR { strings[$0]; next }
{
for (string in strings)
if ( !index($0,string) )
exit 1

}
' file_of_strings RS='^$' file_to_be_searched


and for regexps it'd be:



awk '
NR==FNR { regexps[$0]; next }
{
for (regexp in regexps)

if ( $0 !~ regexp )
exit 1
}
' file_of_regexps RS='^$' file_to_be_searched


If you don't have GNU awk and your input file does not contain NUL characters then you can get the same effect as above by using RS='\0' instead of RS='^$' or by appending to variable one line at a time as it's read and then processing that variable in the END section.



If your file_to_be_searched is too large to fit in memory then it'd be this for strings:




awk '
NR==FNR { strings[$0]; numStrings=NR; next }
numStrings == 0 { exit }
{
for (string in strings) {
if ( index($0,string) ) {
delete strings[string]
numStrings--
}
}

}
END { exit (numStrings ? 1 : 0) }
' file_of_strings file_to_be_searched


and the equivalent for regexps:



awk '
NR==FNR { regexps[$0]; numRegexps=NR; next }
numRegexps == 0 { exit }

{
for (regexp in regexps) {
if ( $0 ~ regexp ) {
delete regexps[regexp]
numRegexps--
}
}
}
END { exit (numRegexps ? 1 : 0) }
' file_of_regexps file_to_be_searched


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...