I need to make a assign shell values from DB queries

DB_name      | IP     | STATUS
---------|-----------|-------
DB_1NAME |  .X.XX.XX.X | DOWN
DB_2NAME |  .X.XX.XX.X | UP

I am querying a database and getting db_name if it is down.

I want to go line by line and grab db name from a row and if it is down save that name to a variable. and nothing else. How do I accomplish this.

IT is quite critical that the db name not contain anything else like spaces etc because I need to also assign the IP next to DB name to a variable HOST_DB1

i'm thinking something like this might do it but I need to be very careful so anyone got any thoughts.

****DB_NAME CAN CONTAIN EXTRA "SPACES" but IP CAN NOT ...


for DB_NAME in $VARIABLE_NAME*
do
    DB_NAME1=${VARIABLE_NAME#*DB_NAMEHERE}
    IP_NAME=${DB_NAME1%%_*}
<<<<HERE I NEED TO GET RID OF EXTRA 0s from IP and have ip cleanly saved.



if...Whatever HERE
fi

If the above is right track how do I isolate each line to a variable
DB result is posted like all at once like so to a log file.

 DB_name      | IP     | STATUS
---------|-----------|-------
DB_1NAME |  .X.XX.XX.X | DOWN
DB_2NAME |  .X.XX.XX.X | UP

---------- Post updated at 03:59 PM ---------- Previous update was at 03:56 PM ----------

SO as above $VARIABLE_NAME can be a line from results.

In shell script (bash/ksh) :

OIFS=$IFS
IFS=' |'
while read db ip status
do
    ip=${ip//.0/.}
    ip=${ip//.0/.}
    ip=${ip#0}
    ip=${ip#0}
    [ "$status" = "DOWN" ] && echo $db $ip
done < infile
IFS=$OIFS

Using awk:

awk -F '[| ]*' '$3=="DOWN" {
    gsub(/\.0+/, ".", $2); gsub(/^0+/, "", $2); print $1,$2}' infile
1 Like

chubler can you show me an example lets say ip was 127.0.0.1 etc I really need an ip example as I still dont know how to use the #. and //.0/ functions well.

say ip is like 127.0.0.1 can you assist. Thanks!

-I will remove post if I figure it out before your help :slight_smile:

---------- Post updated at 11:38 AM ---------- Previous update was at 11:35 AM ----------

FOr instance heres my example ips:

 DB_name      | IP     | STATUS
---------|-----------|-------
DB_1NAME |  127.0.0.1 | DOWN
DB_2NAME |  135.8.8.1 | UP
DB_3NAME |  145.0.0.1 | UP

---------- Post updated at 11:50 AM ---------- Previous update was at 11:38 AM ----------

OIFS=$IFS
IFS=' |'
while read db ip status
do
    ip=${ip#*DB_1NAME |  }
    ip=${ip%% | *}
[ "$status" = "UP" ] && echo $db $ip
done < infile
IFS=$OIFS

using your idea and then converting the ip scrapping techniqe to cut off using the above method I was able to get ips properly returned.

However I don't think its reliable :S can you show me how to use your script and append it for ips like above .

Just remove the ip modification part:

while read db ip status
        do [ "$status" = "DOWN" ] && echo $db $ip
        done < infile

db has the DB_NAME and ip has the corresponding ip address.