2012-10-15 4 views
-2

내 요구 사항은 F-5 구성을 살펴 보는 것입니다. 같은bash, 그렇다면 파일을 구문 분석 할 스크립트

뭔가 :

If x=virtual 
grep for virtual | pool | destination 

파일은 다음과 같습니다 bash

virtual vs_website_443 { 
snat automap 
pool pl_website_443 
destination 11.11.11.11:https 
ip protocol tcp 
persist pr_cookie_JSESSION_AP 
profiles { 
    oneconnect-ebiz-blah {} 
    pr_http_ebiz_x_forwarded_for {} 
    serverssl { 
     serverside 
    } 
    tcp-lan-optimized {} 
    wildcard.origin.website.com { 
     clientside 
    } 
} 
+0

그것은 일부처럼 보이는 당신의 서식이 엉망이 됐어. 자신이하려는 일을 정확하게 파악하는 것은 어렵습니다. 질문에 시간을 할애 해 주시겠습니까? –

+0

또한 예상되는 결과는 무엇입니까? – chepner

+0

당신이 원한다면 :'[[ "$ x"== "virtual"]]; 다음 grep "가상 \ | 풀 \ | 대상"파일; fi'? – c00kiemon5ter

답변

0

일반하지만 쉽게 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 
+0

나는 전체 파일 변경 <파일에 대해 실행하는 경우에 antipentium

+0

아니라, 당신이로 준 데이터의 형식을 기반으로 예를 들어 작동해야합니다. 실제 데이터 형식이 예제와 같은 방식입니까? 예제에 존재하지 않는 선행 공백이 있습니까? – c00kiemon5ter

+0

가상 vs_XXX_www_443 { SNAT의 오토 대상 xx.xx.xx.xx : HTTPS IP 프로토콜 TCP는 규칙은 프로파일 { pr_http_ebiz_x_forwarded_for {} serverssl { 서버 측 } TCP-LAN에 최적화 된 {} 와일드 카드를 short_redirect_xxx_secure. xxx.com { 클라이언트 측 } } } 가상 vs_xxx_www_80 { SNAT 오토 대상 xx.xx.xx.XX : HTTP IP 프로토콜 TCP 규칙은 가 pr_source_addr_avs에게 프로파일 { pr_http_ebiz_x_forwarded_for {} TCP-LAN에 최적화 된 {} – antipentium