我会尝试使用grep和while命令来报告找到的行。
我知道你可以使用以下命令来从inputs.txt文件中比较一系列字符串,并在目标文件中查找它们:
grep -f inputs.txt file_to_check
我希望能够循环读取输入字符串的每一行并进行grep操作。
因此,我尝试了以下方法:
cat inputs.txt | while read line; do if grep "$line" filename_to_check; then echo "found"; else echo "not found"; fi; done
当我将输出重定向到文件时,这不会返回任何内容。
while read line
do
if grep "$line" file_to_check
then echo "found"
else
echo "not found"
fi
done < inputs.txt
与第一个类似,但我发现这种方式更好。
我知道它逐行迭代,因为我可以用echo $line替换grep并打印每一行;但任何一种方法都不像上面的grep -f那样返回任何内容,而是显示:
not found
not found
not found
.
. etc.
我需要的是一个能够迭代每一行并使用if语句通过grep检查它是否被找到的工具。我知道我的逻辑可能不全面,但是我想要的输出应该像这样:
Found *matching line in file_to_check*
Found *matching line in file_to_check*
Not Found $line *(string that does not match)*
.
. etc.
[
。 - karakfa