Storing passing and executing select statement in loop

Hi,

i want to do the following:

Grep the following kind of strings for the 15digit ID which is stored in filename1:

"14:06:51.396 [Aug-21-2015]  INFO  BMCREMEDYSD INPUT-ACTION Failed to retrieve Remedy Incident Modification record: 000000000039047 org.apache.axis2.AxisFault: Read timed out - complete 
 
14:07:50.826 [Aug-21-2015]  INFO  BMCREMEDYSD INPUT-ACTION Failed to retrieve Remedy Incident Modification record: 000000000039048 org.apache.axis2.AxisFault: Read timed out - complete"

I achieved this using the following:

grep -Eo '[0-9]{15}' filename1

But i want this output in another file , say demo.txt

expected output of demo.txt:

000000000039047 
000000000039048 

Now, if we are able to get this , i want to pass the contents of this file, line by line, ID by ID to an SQL select Statement, using for loop:
For eg:

for each line in (demo.txt)
do
select * from tablename where ID=(content of demo.txt)
done

And while doing this we want to store the select staement's output in some variable. Then use this final variable for further processing.

please help in achieving this.

Hello Khushbu,

Kindly use code tags as per forum rules while posting any command/codes/Inputs in your posts, following may help you in same.

 grep -Eo '[0-9]{15}' filename1 > demo.txt
 ### To print only the select statements following.
 awk '{print "select * from tablename where ID= " $0}' demo.txt
  
 ### To take output in a output file if happy with above print command.
 awk '{print "select * from tablename where ID= " $0}' demo.txt > output.txt
  
 ### In case you need your id inclosed within ' then following may help.
 awk -vs1-"'" '{print "select * from tablename where ID= " s1 $0 s1}' demo.txt
 

Hope above helps you.

Thanks,
R. Singh

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

2 Likes

Thanks for the quick reply Ravinder.

When i execute the following in my script (named demon_script.sh):

awk '{print "select * from apsdi-apaction where request_id= " $0}' demo.txt 

i get following error in output file:

SP2-0553: Illegal variable name "=000000000019879".
SP2-0223: No lines in SQL buffer.
SP2-0734: unknown command beginning "grep -Eo '..." - rest of line ignored.
SP2-0734: unknown command beginning "awk '{prin..." - rest of line ignored.
 
no rows selected

how do i troubleshoot this?

1 Like

Show us more of your script please, we can't tell what's going on without knowing the context.

Well, I'm now trying the following script where I'm trying to run a .sql file from this script. But when i open the output file everything is blank. Why is this so?

Script:

PATH=..............
export PATH
ORACLE_HOME=...............
export ORACLE_HOME
TNS_ADMIN=...................
export TNS_ADMIN
cut -d " " -f15 demon | sort -n -r > /home/remedy/demo.txt
awk '{print "select * from apsdi-apaction where request_id= "$0}' /home/remedy/demo.txt > /home/remedy/output.sql
sqlplus -S user/pwd@databasename.co > /home/remedy/variable.txt << EOF
@output.sql
EOF

1.demon file contains the string which I'm able to cut for the 15 digit ID.

2.demo.txt file contains the list of IDs as follows:

000000000019879
000000000039043

3.output.sql file contains following:

select * from apsdi-apaction where request_id= 000000000039043
select * from apsdi-apaction where request_id= 000000000019879

4.variable.txt file should contain the output of above sql statements, but the file appears blank.

Please let know what changes are to be made in above script to make it work. How do i excute the .sql file from above script. Any other alternative will also work.

There are two issues I see:
A SQL-Statement is terminated by a semicolon, which seems to be missing in output.sql.
A minus sign should be avoided in table names. If it is present the table name has to be put into double quotes. The double quotes make the reference to the table case sensitive, so make sure you reference it the way it was created (the user_tables view tells you the correct name).

Hi Cero,

Thanks for debugging. Worked on tablename issue.

Now how do i add semicolon to output.sql since it is being written using the awk command as follows:

awk '{print "select * from apsdi-apaction where request_id= "$0}' /home/remedy/demo.txt > /home/remedy/output.sql

Hello Khushbu,

Following a minor change in code may help you in same.

 awk '{print "select * from apsdi-apaction where request_id= "$0 ";"}' /home/remedy/demo.txt > /home/remedy/output.sql
 

Thanks,
R. Singh

1 Like

Try ... where request_id= "$0 ";" ...
Two comments:

  • if the leading zeroes are mandatory, shouldn't the numbers be single quoted?
  • why not use ... where request_id in ( - contents of demo.txt here - ); ?