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
|