plain bash
,虽然可以很容易地转换为 POSIX sh。
#!/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