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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to take input from two files in a UNIX script?
# 1  
Old 03-14-2013
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 02:11 PM..
# 2  
Old 03-14-2013
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

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-14-2013
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
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 03:21 PM..
# 5  
Old 03-14-2013
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"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Building a dynamic UNIX menu with input files

Hi! I am looking to build dynamic menu (named: lookup.sh) that reads a pipe delimited file for input. for example, contents of input.txt could be: user1|srv1 user3|srv1 user4|srv1 user2|srv2 I want the menu look like: 1) get password for user1 on srv1 2) get password for user3 on... (7 Replies)
Discussion started by: cpolikowsky
7 Replies

2. Shell Programming and Scripting

Standardization of input source data files using shell script

Hi there, I'm a newbie in unix and am fishing for options related to how raw input data files are handled. The scenario, as I'm sure y'all must be very familiar with, is this : we receive upwards of 50 data files in ASCII format from various source systems - now each file has its own structure... (3 Replies)
Discussion started by: Prat Khos
3 Replies

3. Shell Programming and Scripting

Help in unix script to join similar lines of input

Hi, I have been thinking of how to script this but i have no clue at all.. Could someone please help me out or give me some idea on this? I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas. Let's say i have the following input. ... (3 Replies)
Discussion started by: rei125
3 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

Perl Script Not Reading Input Files Correctly

This is one of the strangest things that's happening to me. I'm writing a new Perl script that is trying to read a file. The file is originally in .mof format, but I also saved the contents into a .txt file. As a simple test, I wrote this: #!/user/bin/perl -w use strict; ... (3 Replies)
Discussion started by: kooshi
3 Replies

6. Shell Programming and Scripting

How to Process input files from folder in shell script?

Hi, I want to process all input files available into folder (C:\ShellPrg\InputFile\) Input files are abc.CSV , XYZ.zip (zip of CSV file), PQR.gz (zip of CSV file). I want to check the extension of file, If its .zip/.gz then need to unzip the file as .CSV I want to parse line by line of... (2 Replies)
Discussion started by: Poonamol
2 Replies

7. Shell Programming and Scripting

How to get this script work on multiple input files

Hello Gyues! I would like to use awk to perform data extraction from several files. The data files look like this: DWT26R 1 PEP1 CA 1 OH2 SKIPPED: 0 STEP: 1 0.29000E+01 0.55005E-02 0.60012E-03 0.30000E+01 0.11149E+00 0.13603E-01 0.31000E+01 0.39719E+00 0.63013E-01 0.32000E+01... (9 Replies)
Discussion started by: Daniel8472
9 Replies

8. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies

9. Shell Programming and Scripting

Script to find files on a given input date

Hello gurus, I need to write a script to find out all the file that got changed on a specific folder since a given input date (Date to be given as Input) Thanx (1 Reply)
Discussion started by: ar.karan
1 Replies
Login or Register to Ask a Question