How to take input from two files in a UNIX script? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to take input from two files in a UNIX script?

UNIX for Dummies Questions & Answers


Tags
sql, unix

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-14-2013
Sanjeev Yadav Sanjeev Yadav is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 14 March 2013, 2:03 PM EDT
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
How to take input from two files in a UNIX script?

I have a simple script which returns me the response from a table.

Code:
while read -r xx
do
db2 -x "select * from Table where user ='$xx' with ur " >> "/home/Results.txt"
done < "/home/USERS.txt"


Now I want to have two different clause in the where clause and want the script to take input from two files or same file with delimiters ...how do I input the yy variable value to the query below ???????


Code:
do
db2 -x "select * from Table where user ='$xx' and client = '$yy' with ur " >> "/home/Results.txt"
done < "/home/USERS.txt"


Last edited by Corona688; 03-14-2013 at 01:11 PM..
Sponsored Links
    #2  
Old 03-14-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 25 July 2014, 4:11 PM EDT
Location: Saskatchewan
Posts: 18,978
Thanks: 748
Thanked 3,146 Times in 2,951 Posts
Same file with delimiters:


Code:
while IFS="," read VAR1 VAR2 THEREST
do
        ...
done < inputfile

Two files:


Code:
exec 5<file1 # Open file into FD 5
exec 6<file2

while read VAR1 <&5 && read VAR2 <&6
do
        ...
done

exec 5<&- # Close FD 5
exec 6<&-

One file would obviously be preferable.

Also, you don't need to reopen Results.txt 9,000 times to write 9,000 lines to it. You can redirect the output of your loop the same way you redirect its input.


Code:
while ...
do
...
done > results.txt

The Following User Says Thank You to Corona688 For This Useful Post:
Sanjeev Yadav (03-14-2013)
Sponsored Links
    #3  
Old 03-14-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 July 2014, 8:04 AM EDT
Location: San Jose, CA, USA
Posts: 4,149
Thanks: 162
Thanked 1,414 Times in 1,199 Posts
Sanjeev,
You have posted the same question to two different forums and received different answers to your posts. You shouldn't post to multiple locations because the responses to your posts will likely not realize that two conversations are going on for this single topic.

Please don't do this again in the future. Please ask the administrators to merge these two discussions into a single thread and to close the other one!
    #4  
Old 03-14-2013
Sanjeev Yadav Sanjeev Yadav is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 14 March 2013, 2:03 PM EDT
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
I tried running the below script but it doesn't work for me....can you fix this for me....Sorry, I am new to unix..


Code:
exec 5< "/home/epprod/COSMOS_CUSTOME_CLIENTID.txt"
exec 6< "/home/epprod/COSMOS_CUSTOME_CLIENTIDS.txt"
while read VAR1 <&5 && read VAR2 <&6
do
db2 -x "select customer_nbr,client_id from policies where customer_nbr='VAR1' and client_id='VAR2' with ur " >> "/home/epprod/COSMOS_CUSTOME_CLIENTID_Results.txt"
done
exec 5<&-
exec 6<&-


Last edited by Corona688; 03-14-2013 at 02:21 PM..
Sponsored Links
    #5  
Old 03-14-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 25 July 2014, 4:11 PM EDT
Location: Saskatchewan
Posts: 18,978
Thanks: 748
Thanked 3,146 Times in 2,951 Posts
You are one infraction away from being set read-only for a while, please listen to the messages and warnings we're giving you. Don't double-post, be sure to code-tag your posts, and do NOT pm people with technical questions or to 'hurry them up'. We are not on call.



Code:
while read VAR1 <&5 && read VAR2 <&6
do
db2 -x "select customer_nbr,client_id from policies where customer_nbr='$VAR1' and client_id='$VAR2' with ur " 
done > "/home/epprod/COSMOS_CUSTOME_CLIENTID_Results.txt"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help in unix script to join similar lines of input rei125 Shell Programming and Scripting 3 06-29-2012 03:55 AM
Script to delete files with an input for directories and an input for path/file *ShadowCat* Shell Programming and Scripting 3 06-28-2012 09:16 PM
How to get this script work on multiple input files Daniel8472 Shell Programming and Scripting 9 08-28-2010 08:43 AM
Write a new file from 2 files as input to the script sniper57 Shell Programming and Scripting 5 06-11-2009 05:14 AM
Script to find files on a given input date ar.karan Shell Programming and Scripting 1 08-13-2007 10:07 PM



All times are GMT -4. The time now is 11:10 AM.