Sponsored Content
Top Forums Shell Programming and Scripting Connect to database with shell output Post 303008034 by Don Cragun on Sunday 26th of November 2017 03:44:27 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy