コンソールからパターンを検索して出力する

コンソールからパターンを検索して出力する

入力ファイルは次のとおりです。

{"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 指定された行のすべての一致を印刷します。

おすすめ記事