I need to make a assign shell values from DB queries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I need to make a assign shell values from DB queries
# 1  
Old 10-02-2014
I need to make a assign shell values from DB queries

Code:
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.

Code:
****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.

Code:
 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.
Moderator's Comments:
Mod Comment Please use CODE tags for sample input, sample output, and code segments.

Last edited by Don Cragun; 10-02-2014 at 05:38 PM.. Reason: Add CODE tags.
# 2  
Old 10-02-2014
In shell script (bash/ksh) :

Code:
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:
Code:
awk -F '[| ]*' '$3=="DOWN" {
    gsub(/\.0+/, ".", $2); gsub(/^0+/, "", $2); print $1,$2}' infile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 10-03-2014
Quote:
Originally Posted by Chubler_XL
In shell script (bash/ksh) :

Code:
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:
Code:
awk -F '[| ]*' '$3=="DOWN" {
    gsub(/\.0+/, ".", $2); gsub(/^0+/, "", $2); print $1,$2}' infile

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 Smilie

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

FOr instance heres my example ips:
Code:
 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 ----------

Code:
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 .
# 4  
Old 10-03-2014
Just remove the ip modification part:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign awk values to shell variable?

Hi Gurus, I have a script which assign awk output to shell variable. current it uses two awk command to assign value to two variables. I want to use one command to assign two values to two variables. I tried the code, but it does't work. kindly provide your suggestion. current code... (2 Replies)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. UNIX for Dummies Questions & Answers

How to assign values to variable in given scenario

Hi i have one variable like DIR="f1 f2" in config file in my script i have one runtime variable LFILE="DIR" now i want to use $DIR in my script by using LFILE that is i dont want to use DIR dirctly i am extracting DIR by some other means. Config file : DIR="f1 f2" Script: LFILE="DIR" i... (3 Replies)
Discussion started by: sriram_gec
3 Replies

6. Shell Programming and Scripting

Assign values to variable

Hi Masters, I want to assign the values of one variable to another variable. Here the varaible name 'var' is dynamic. I know the values of V_2 and U_3, but If the i/p of TYPE is 'U' and the NO is 3, then I want to assign the values of U_3 to var. How we can achieve it? TYPE="U"... (4 Replies)
Discussion started by: ecearund
4 Replies

7. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

8. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

9. Shell Programming and Scripting

assign values from awk output - help

Dear All, I have a command which gives the number of fields of each line of a comma-separated file. sthng like this : cat QDB_20071126_002.bad | awk -F"," '{ print NF }' I need to assign the first output and the last output of the above command to variables in a script. Need help to do... (4 Replies)
Discussion started by: KrishnaSaran
4 Replies

10. UNIX for Dummies Questions & Answers

to assign cut values to an array

i need to seperate values seperated by delimiters and assign it to an array.. can u plz help me on that. Variables = "asd,rgbh,(,rty,got,),sroe,9034," i need to assign the variables into arrays.. like.. var=asd var=rgbh.. and so on how do i do this. i need to reuse the values stored in... (6 Replies)
Discussion started by: Syms
6 Replies
Login or Register to Ask a Question