假设你只想打印包含所有字符串的文件名,这是一个解决方案,适用于任意数量的字符串,并进行字符串比较,而不是正则表达式比较:
gawk -v RS='\0' -v strings="string1 string2" '
BEGIN{ numStrings = split(strings,stringsA) }
{
matchCnt = 0
for (stringNr=1; stringNr<=numStrings; stringNr++)
if ( index($0,stringsA[stringNr]) )
matchCnt++
}
matchCnt == numStrings { print FILENAME }
' file*
稍等,我刚刚注意到你想要打印不包含2个字符串的文件。那应该是:
gawk -v RS='\0' -v strings="string1 string2" '
BEGIN{ numStrings = split(strings,stringsA) }
{
matchCnt = 0
for (stringNr=1; stringNr<=numStrings; stringNr++)
if ( index($0,stringsA[stringNr]) )
matchCnt++
}
matchCnt == numStrings { matchesAll[FILENAME] }
END {
for (fileNr=1; fileNr < ARGC; fileNr++) {
file = ARGV[fileNr]
if (! (file in matchesAll) )
print file
}
}
' file*
打印不包含任何字符串的文件名将是:
gawk -v RS='\0' -v strings="string1 string2" '
BEGIN{ numStrings = split(strings,stringsA) }
{
for (stringNr=1; stringNr<=numStrings; stringNr++)
if ( index($0,stringsA[stringNr]) )
matchesOne[FILENAME]
}
END {
for (fileNr=1; fileNr < ARGC; fileNr++) {
file = ARGV[fileNr]
if (! (file in matchesOne) )
print file
}
}
' file*
-L
选项吗(-l
显示文件内容与给定正则表达式匹配的文件)? - chepner