i have yet another question regarding bash.

in a text file, there is a set of information. the constant here is the "sys app" and the end "checker"

sys app /partition/app_webapps_80 {
load-balancing-mode least-connections-member
members {
    /partition/host-1:80 {
    /partition/host-2:80 {
checker /partition/tcp 
sys app /partition/app_webapps_8989 {
    members {
        /partition/host-1:8989 {
        /partition/host-2:8989 {
    checker /partition/tcp 

what i did is to create a while loop, then i uses the following, however i get the address replicated the number of lines in the file.


while read do rs=$(sed -n '/sys app/,/checker/p' filename |grep -m $line "address" |tail -n 1) 
echo `$rs 
line=$[$line +1]`

done < filename

what should i go for, if i just want to match some information in between 2 string, then assign it to a variable that i can use for later?

appreciate any advise on this.

since under app_webapps_80 and app_webapps_8989 they have the same address and address

the expected result is to run the loop. and the loop will run to identify how many address there are between app_webapps_80 and the first checker

then assign the address to a variable to use and then assign address to another variable to use

the loop will run again to identify the new app_webapps_8989 and the checker in that section repeat what it does for the first set.

not sure if i make sense on this?


Instead of doing a range match, I think it will be easier to just read it from top to bottom.


declare -A A=()

while read -r b c; do
done < <(awk '
    /^ *sys app /{b=substr($3,12)}
    /^ *address /{a[b]=sprintf("%s %s",a[b],$2)}
    END {
        for(c in a){print c a[c])}
    }' file)

# Let's iterate the assosiative array.
for d in ${!A[@]}; do
    set -- ${A[$d]}
    echo 1st   - $d: $1
    echo 2nd   - $d: $2
    echo All   - $d: $@
    echo Last  - $d: ${@:(-1)}
    echo Range - $d: ${@:1:2}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.