콘솔에서 패턴 검색 및 출력 출력

콘솔에서 패턴 검색 및 출력 출력

입력 파일은 다음과 같습니다.

{"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 주어진 줄에 있는 모든 일치 항목을 인쇄합니다..

관련 정보