LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-19-2009, 02:29 PM   #1
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

Rep: Reputation: 30
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
 
Old 11-19-2009, 06:56 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Is there a question, or are we simply celebrating your success, here?
 
Old 11-20-2009, 10:07 AM   #3
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 198

Original Poster
Rep: Reputation: 30
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...).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
BASH - Array Variables Micro420 Programming 5 12-15-2006 05:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration