搜索模式并在控制台上打印输出

搜索模式并在控制台上打印输出

输入文件如下所示:

{"key":"value";"ipaddress:"scrubbed";"id":"scrubbed"}
{"key1":"value";"ipaddress:"scrubbed";"id":"scrubbed"}
{"key2":"value";"ipaddress:"scrubbed";"id":"scrubbed"}
{"key3":"value";"ipaddress:"scrubbed";"id":"scrubbed"}

设想:

我必须检查上面文件中所有输入行的 IP 地址和 ID 是否已被删除

要在控制台上打印的异常输出:

ip address and id got scrubbed in all the lines of input file

答案1

这假设值“scrubbed”只能针对 id 和 ipaddress 出现。但可能不是最好的解决方案。

#!/bin/bash

flag=0

while read -r line
do
    if [[ $(echo "$line" | grep -o -w scrubbed | wc -l) -ne 2 ]]
    then
        echo 'IP address and id NOT scrubbed in every line!'
        echo "$line"
        flag=1
        break
    fi
done < input.txt

if [[ $flag -ne 1 ]]
then
    echo 'ip address and id got scrubbed in all the lines of input file'
fi

笔记:grep -o 打印给定行中的所有匹配项

相关内容