LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash: use file as input into array, parse out other variables from array using awk (https://www.linuxquestions.org/questions/linux-general-1/bash-use-file-as-input-into-array-parse-out-other-variables-from-array-using-awk-770285/)

beeblequix 11-19-2009 02:29 PM

bash: use file as input into array, parse out other variables from array using awk
 
1. file contains 4 columns --
a. business date
b. transaction date
c. acct#
d. transaction#

Needed to use each field from each line as input to query the database to pull a 5th field.

Found code on this forum to set up an array -- in this case each line is set to a variable. Then I used a while-do-done statement to step through the total number of lines to know when to quit.

****************************************

#!/bin/bash

infile=/home/me/myfile.txt
outfile=/home/me/myoutfile.txt

#this sets up the array based on input
old_IFS=$IFS
IFS=$'\n'
lines=($(cat $infile))
IRS=$old_IFS
line_we_r_on=0
db2 connect to MYDATABASE user MYUSER using SOMEUBERSECRETPASSWORDTHATSNOT\admin\

#the while loop executes until the statement is false
while [ "$line_we_r_on" -lt "${#lines[*]}" ]
do
#setting the variables up like this lets me munch apart the initial array variable into necessary ones
busdt=`echo ${lines[${line_we_r_on}]} | awk '{print $1}'`
trantm=`echo ${lines[${line_we_r_on}]} | awk '{print $2}'`
acctnbr=`echo ${lines[${line_we_r_on}]} | awk '{print $3}'`
trannbr=`echo ${lines[${line_we_r_on}]} | awk '{print $4}'`

# now my DB2 query inside the while loop (note some fields needed single quotes when others didn't
db2 "select SOMEFIELD1, SOMEFIELD2, SOMEFIELD3, SOMEFIELD4, LOCATION from SOMETABLE where SOMEFIELD1='${trantm}' and SOMEFIELD2 like '%${acctnbr}%' and SOMEFIELD3 =${trannbr}" >> $outfile
#this line increments the one value until it's equal to the total number in array value and quits once it gets there.
line_we_r_on=$[line_we_r_on + 1]
done

# disconnect from database
db2 connect reset
db2 quit

jhwilliams 11-19-2009 06:56 PM

Is there a question, or are we simply celebrating your success, here?

beeblequix 11-20-2009 10:07 AM

There is no question -- I posted this for posterity (& as a clever place out there in 'the cloud' for my memories -- kinda like Dumbledore's memory-thingamajig...).


All times are GMT -5. The time now is 03:56 AM.