일반하지만 쉽게 POSIX 쉬로 변환 할 수 있습니다.
#!/usr/bin/env bash
in=0 # whether we are inside a 'virtual' block
# such a block ends once we meet a line that starts with '}'
while read -r
do
if [[ $REPLY =~ ^virtual ]]; then
in=1
echo "${REPLY% *}"
elif ((in)); then
if [[ $REPLY =~ ^pool ]]
then echo "$REPLY"
elif [[ $REPLY =~ ^destination ]]
then echo "${REPLY%:*}" # or just "$REPLY" if you want the ':https' part
elif [[ $REPLY =~ ^} ]]
then in=0
fi
fi
done < file
곳 file
은 당신의 데이터입니다. 이를 "$1"
으로 변경하고 파일을 스크립트의 인수로 제공 할 수 있습니다. 주어진 데이터로 테스트
는
, 반환
virtual vs_website_443
pool pl_website_443
destination 11.11.11.11
주어진 데이터 출력과 일반 awk
awk '$1 == "virtual" { f=1; print $1,$2; next } \
f == 1 { if ($1 == "pool") { print } \
else if ($1 == "destination") { print } \
else if ($0 ~ /^}/) { f=0 } \
}' file
이다 사용 :
$ awk '$1 == "virtual" { f=1; print $1,$2; next } f == 1 { if ($1 == "pool") { print } else if ($1 == "destination") { print } else if ($0 ~ /^}/) { f=0 } }' file
virtual vs_website_443
pool pl_website_443
destination 11.11.11.11:https
그것은 일부처럼 보이는 당신의 서식이 엉망이 됐어. 자신이하려는 일을 정확하게 파악하는 것은 어렵습니다. 질문에 시간을 할애 해 주시겠습니까? –
또한 예상되는 결과는 무엇입니까? – chepner
당신이 원한다면 :'[[ "$ x"== "virtual"]]; 다음 grep "가상 \ | 풀 \ | 대상"파일; fi'? – c00kiemon5ter