Connect to database with shell output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Connect to database with shell output
# 1  
Old 11-26-2017
Connect to database with shell output

Dear All,

I am trying to write a script which will generate output to a file based on input files on basis of a pattern which I am able to do so. But, I am stuck as I am trying to connect this to database server. Below is my command and output details.

Input Code

Code:
filedate=$(date +"%d%m%Y")

cat  $filedate*lpn* | grep "H|" | cut -c9-26|sort|uniq > $path/alllpnfile.dat

File output.

Code:
990099110012345678
990099110012345666
990099110012345645
990099110012345701
220022110012345678
220022110012345690


Now, I want to read this new file and write a select statement in SQL by reading the complete file. But the only condition is that this newly created file contains details as above and I need to use comma to include all the lines in the file so that I will be able to use a single select statement in sql.

Now I need to use these lines from alllpnfiles.dat into a single select statement as below.

Code:
select * from tablename where sdigits in
(990099110012345678,
990099110012345666,
990099110012345645,
990099110012345701,
220022110012345678,
220022110012345690)

Regards.
GVK
Moderator's Comments:
Mod Comment Note that this thread has been moved from the "How to Post in the The UNIX and Linux Forums" forum to the "Shell Programming and Scripting" forum.

Last edited by Don Cragun; 11-26-2017 at 04:53 AM.. Reason: Note forum change.
# 2  
Old 11-26-2017
Quote:
Originally Posted by grvk101
Dear All,

I am trying to write a script which will generate output to a file based on input files on basis of a pattern which I am able to do so. But, I am stuck as I am trying to connect this to database server. Below is my command and output details.

Input Code

Code:
filedate=$(date +"%d%m%Y")

cat  $filedate*lpn* | grep "H|" | cut -c9-26|sort|uniq > $path/alllpnfile.dat

File output.

Code:
990099110012345678
990099110012345666
990099110012345645
990099110012345701
220022110012345678
220022110012345690


Now, I want to read this new file and write a select statement in SQL by reading the complete file. But the only condition is that this newly created file contains details as above and I need to use comma to include all the lines in the file so that I will be able to use a single select statement in sql.

Now I need to use these lines from alllpnfiles.dat into a single select statement as below.

Code:
select * from tablename where sdigits in
(990099110012345678,
990099110012345666,
990099110012345645,
990099110012345701,
220022110012345678,
220022110012345690)

Regards.
GVK
Your pipeline includes a sort, but your output clearly is not sorted???

So, assuming that the output does not need to be sorted, we can replace the rest of the pipeline with a single invocation of awk:
Code:
filedate=$(date '+%d%m%Y')
awk '
/H\|/ {	pn[substr($0, 9, 18)]
}
END {	print "select * from tablename where sdigits in"
	sep = "("
	for(i in pn) {
		printf("%s%s", sep, i)
		sep = ",\n"
	}
	print ")"
}' $filedate*lpn*

But, of course, since no sample input was provided, the above code is completely untested. If, despite your sample output, you really do need the output to be sorted, you can modify the for loop in the END clause in the above awk code to feed the list to sort and feed the output from sort into another for loop to print the results. Or, the awk available on some systems provides a function that could be used to sort the pn[] array before printing the results.

And, you haven't told us what operating system or shell you're using. If you're using a Solaris/SunOS system, change awk in the above code to /usr/xpg4/bin/awk or nawk.

Last edited by Don Cragun; 11-26-2017 at 05:01 AM.. Reason: Fix tabs.
# 3  
Old 11-27-2017
Dear Don,

Thnx a lot for your quick response.

Below is the set -x otput I am getting for the code.

Code:
filedate=$(date '+%d%m%Y')awk '/H\|/ {	pn[substr($0, 9, 18)]}END 
{	print "select * from tablename where sdigits in"
	sep = "("
	for(i in pn) {
		printf("%s%s", sep, i)
		sep = ",\n"
	}
	print ")"
}' $filedate*lpn* > $path/all_file.dat

Output

Code:
+ /H\|/ {       pn[substr($0, 9, 18)]}END
{       print "select * from tablename where sdigits in"
        sep = "("
        for(i in pn) {
                printf("%s%s", sep, i)
                sep = ",\n"
        }
        print ")"
} 27112017181509.271117181509_dscinlpnptl.dat.gz
+ + date +%d%m%Y
filedate=27112017awk
./ptl_lpn_new.sh[24]: /H\|/ {^Ipn[substr($0, 9, 18)]}END ^J{^Iprint "select * from tablename where sdigits in"^J^Isep = "("^J^Ifor(i in pn) {^J^I^Iprintf("%s%s", sep, i)^J^I^Isep = ",\n"^J^I}^J^Iprint ")"^J}:  not found

The command is able to fetch that file but everything after that has failed.

Regards.
GRVK
# 4  
Old 11-27-2017
You seem to
- be missing a line terminator between the filedate assignment and the awk command.
- operate on a gzipped file, which is not *nix text - awk sensibly works on text only. gunzip first.

Last edited by RudiC; 11-27-2017 at 11:39 AM.. Reason: typo...
# 5  
Old 11-27-2017
Dear RUDIC,

Thnx for the edit.

@DON,

I made changes and the script is executing. But the output which I am getting is not a single in but in iterations.
The input file has 3 lines and output generated is as follows.

OUTPUT

Code:
select * from tablename where sdigits in
(990099110300112517)
select * from tablename where sdigits in
(990099110300116099,
990099110300112517)
select * from tablename where sdigits in
(990099110300116094,
990099110300116099,
990099110300112517)

The above output is from a single file only.

Regards.
GVK
# 6  
Old 11-27-2017
I would suggest to use SQL*Loader to load data into a temp table and query table.

Or if you have privileges to use UTL_FILE package, then you can write a PL/SQL program to read text files and query table.
# 7  
Old 11-27-2017
Quote:
Originally Posted by grvk101
Dear RUDIC,

Thnx for the edit.

@DON,

I made changes and the script is executing. But the output which I am getting is not a single in but in iterations.
The input file has 3 lines and output generated is as follows.

OUTPUT

Code:
select * from tablename where sdigits in
(990099110300112517)
select * from tablename where sdigits in
(990099110300116099,
990099110300112517)
select * from tablename where sdigits in
(990099110300116094,
990099110300116099,
990099110300112517)

The above output is from a single file only.

Regards.
GVK
Please try using the code I suggested in post #2:
Code:
filedate=$(date '+%d%m%Y')
awk '
/H\|/ {	pn[substr($0, 9, 18)]
}
END {	print "select * from tablename where sdigits in"
	sep = "("
	for(i in pn) {
		printf("%s%s", sep, i)
		sep = ",\n"
	}
	print ")"
}' $filedate*lpn*

exactly as shown above without removing line breaks and without adding line breaks that are in the above suggestion. There is a HUGE difference in the behavior of:
Code:
END { ... }

and:
Code:
END
{ ... }

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to connect to Oracle database using shell script?

Hi All, I am newbie to unix shell scripting. I want to connect to oracle database using shell script. I know how to connect DB using user name and password but my question is if my password is having '@' sign then how to connect. I have tried like below, cnt=`sqlplus -s /nolog << EOFSQL ... (3 Replies)
Discussion started by: pmotewar
3 Replies

2. Shell Programming and Scripting

Shell Script to connect to the oracle database

Hi Unix Gurus, I have a requirement to write a script, Oracle DB gets referesh every 6hrs, hence we need write a script to connect to the database , if it connects it should generate a file to proceed the next job application or when an error when connectiong to DB it should not generate any... (8 Replies)
Discussion started by: bshivali
8 Replies

3. Shell Programming and Scripting

Shell Linux to connect to a database and execute store procedure

HI, i want to write a script (Linux) that: 1) connect to a database oracle 2) execute some store procedure. Can anybody help me, please? Thanks a lot Andrew (3 Replies)
Discussion started by: manichino74
3 Replies

4. UNIX for Dummies Questions & Answers

Shell scripting+connect to oracle database+spooling

Hi! Everyone I am new to the shell scripting basically.I have been asked to create a shell script that connect to a oracle database to read data from a particular schema then spool it into a csv file then email to customer. Can anybody let me know how to go about that. I have create... (14 Replies)
Discussion started by: Mr Mo
14 Replies

5. Shell Programming and Scripting

how will i connect postgres database from unix shell script

I have got the solution so just closing this issue. (3 Replies)
Discussion started by: jambesh
3 Replies

6. Shell Programming and Scripting

How to connect to database db2 through Unix Shell Script

I am trying to connect to database db2 through shell script. The process I am trying is > db2 It gives me error Access Denied. While checking for access I have the rights. Is there ant other way round..? Please help. (3 Replies)
Discussion started by: ankitgupta
3 Replies

7. IP Networking

how you connect to a database

i am working on informix database can any body helps me with coding how to connect to it on solaris (3 Replies)
Discussion started by: ramneek
3 Replies

8. Shell Programming and Scripting

Connect to sybase database using Korn shell script

Hi, Can anyone please give me a script or let me know how to connect to a sybase database and execute a query using Korn shell scripts.Am new to Unix but i need to do this ASAP. Please help. Thanks, Gops (7 Replies)
Discussion started by: bhgopi
7 Replies

9. Programming

How to connect database in TC

Anybody please help me how connect a database file in TC for windows. regards Senthil. K (5 Replies)
Discussion started by: Senthil
5 Replies

10. Shell Programming and Scripting

Can i connect from a .sh or .sc to a database?

please, help me-.... i need connect to a database Sybase IQ 12 and I don't know if i can do it using a shell program. if it's posibble, please tell me how... please, help me, because I don't know what to do.... thanks... jonathan (1 Reply)
Discussion started by: DebianJ
1 Replies
Login or Register to Ask a Question