How to remove characters for sql query output?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove characters for sql query output?
# 1  
Old 11-24-2013
How to remove characters for sql query output?

Hi,

my sql query return below value for " $SQL_OPN_AMT "

Code:
CUSTCODE OH OHOPNAMT_GL OHREFNUM -------------------------------- -- ----------- ------------------------------ OHENTDATE FCC --------- --- 1.1072256 IN 74.85 000072061
0 01-MAY-13 MVR 1.1072256 IN 751.72 0000744751 01-JUN-13 MVR 1.1072256 IN 382.19 0000790408 01-AUG-13 MVR CUSTCODE OH OHOPNAMT_GL OHREFNUM -----------------------------
--- -- ----------- ------------------------------ OHENTDATE FCC --------- --- 1.1072256 IN 589.8 0000768242 01-JUL-13 MVR 1.1072256 IN 216 0000810616 01-SEP-13 MVR
CUSTCODE OH OHOPNAMT_GL OHREFNUM -------------------------------- -- ----------- ------------------------------ OHENTDATE FCC --------- --- 2.38.00.00.100010 IN 948.32
0000856580 01-NOV-13 MVR

I am using below code to display in tabular form.

Code:
echo $SQL_OPN_AMT | /usr/xpg4/bin/awk -F' ' '{for(i=13;i<=NF;i++){printf("%s%s",$i,i%6?" ":"\n")}}' >>  cust_amt_dtls.tmp

but my out put is showing as below.

Code:
1.1072256,DD,74.85,0000720610,01-MAY-13,MVR
1.1072256,DD,751.72,0000744751,01-JUN-13,MVR
1.1072256,DD,589.8,0000768242,01-JUL-13,MVR
1.1072256,DD,382.19,0000790408,01-AUG-13,MVR
1.1072256,DD,216,0000810616,01-SEP-13,MVR
2.38.00.00.100010,DD,948.32,0000856580,01-NOV-13,MVR
-----------,------------------------------,,DDC,---------,---
CUSTCODE,OH,,OHREFNUM,--------------------------------,--

I want to remove the extra lines/characters coming along with the tabular output.

i.e my output should be

Code:
1.1072256,DD,74.85,0000720610,01-MAY-13,MVR
1.1072256,DD,751.72,0000744751,01-JUN-13,MVR
1.1072256,DD,589.8,0000768242,01-JUL-13,MVR
1.1072256,DD,382.19,0000790408,01-AUG-13,MVR
1.1072256,DD,216,0000810616,01-SEP-13,MVR
2.38.00.00.100010,DD,948.32,0000856580,01-NOV-13,MVR


Thanks
Bhavish

Last edited by Scott; 11-24-2013 at 11:10 AM.. Reason: Fixed code tags
# 2  
Old 11-24-2013
grep "\." output

#should give you your output

Last edited by samit11; 11-24-2013 at 03:49 PM..
# 3  
Old 11-24-2013
that's some crappy output. perhaps you should paste the part of the code that creates your $SQL_OPN_AMT. I bet it can be reworked to make this easier rather than fixing the wrong problem. I couldn't reproduce the output with this input. It's slightly formatted wrong.

This will skip from "CUSTCODE" and the next 12 fields
Code:
mute@thedoctor:~/temp/ranabhavish$ awk '{for(i=13;i<=NF;i++){if($i=="CUSTCODE"){i+=11}else{printf("%s%s",$i,i%6?",":"\n")}}}' input
1.1072256,IN,74.85,0000720610,01-MAY-13,MVR
1.1072256,IN,751.72,0000744751,01-JUN-13,MVR
1.1072256,IN,382.19,0000790408,01-AUG-13,MVR
1.1072256,IN,589.8,0000768242,01-JUL-13,MVR
1.1072256,IN,216,0000810616,01-SEP-13,MVR
2.38.00.00.100010,IN,948.32,0000856580,01-NOV-13,MVR

I'd rather fix it the right way though.
# 4  
Old 11-25-2013
Might I suggest that you alter your SQL call to something like this:-
Code:
sqlplus -S <<-EOSQL | read col1 col2 col2 col3 col4 col5 col6 
$user/$pass@$sid
set heading off
set pagesize 0

Your query here

EOSQL

This should get rid of much of the unwanted parts and (assuming that you have only one lines returned) will parse the output into values $col1, $col2, $col3, etc. for you to work with later in your script.

Using the format above hides the credentials from a ps command that anyone could be running. The two set commands turn off the titles and the page split respectively. The -S flag will prevent the output of the usual banner messages when starting SQL.



I hope that this helps. If I've missed the point, please post the section of code you are stuck with, including the SQL call.


Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 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

Sql query output count

Hi Team, below sql rerturn 20 records, the result set i am going to assign to one variable and it showing count is 1. and i don't use count() in sql query... based on count, i need to fail the script. No_of_step=`echo ${g_count} | wc -l` function gf_count() { g_count=`sqlplus -s... (8 Replies)
Discussion started by: bmk123
8 Replies

2. Shell Programming and Scripting

Shell script appending output of sql query

I am writing the following script to create the file v_out.txt. sqlplus -s /nolog << EOF CONNECT scott/tiger@orcl; whenever sqlerror exit sql.sqlcode; set newpage 0; SET PAGESIZE 0; SET ECHO OFF; SET FEEDBACK OFF; SET HEADING OFF; SET VERIFY OFF; SET LINESIZE 100; set tab off; set... (7 Replies)
Discussion started by: itzkashi
7 Replies

3. Shell Programming and Scripting

How to Assign the Output of an SQL Query to a Variable?

Hi iam new to shell scripting how to declare variables as redshift query and I have to compare two counts by using if condition . ex:count=select count(*) from prd; select count(*) from prd; select count(*) from tag; can any one help me . Please use CODE tags when displaying... (1 Reply)
Discussion started by: sam526
1 Replies

4. Solaris

SQL QUERY to Table Output

Hi I am trying to run sql query from solaris in csh script and send the output to email. Below is my sql query select p.spid,se.program seprogram, se.machine, se.username, sq.sql_text,sq.retrows from v$process p inner join v$session se on p.addr = se.paddr inner join ( select... (2 Replies)
Discussion started by: tharmendran
2 Replies

5. Shell Programming and Scripting

SQL Query in Shell Script output formatting

Hi All, #!/bin/ksh call_sql () { sql=$1 sqlplus -s $sqlparam_sieb <<EOF SET ECHO OFF; SET NEWPAGE NONE; SET SQLBL OFF; SET VERIFY OFF; SET LINESIZE 2000; SET... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

6. Shell Programming and Scripting

How to store the sql query output into txt file?

Hi I want ot save SQL query result in one txt file. for that i have written one code line sqlplus -s $dbstring @/usr/local/bin/sched/nightly_Cronjob/exec_123.sql >> /usr/local/bin/sched/nightly_Cronjob/result.txt but it is not working . database : Oracle so please advice me how can i... (7 Replies)
Discussion started by: Himanshu_soni
7 Replies

7. UNIX for Advanced & Expert Users

Output the SQL Query result to a File

Hello Guys, This message is somewhat relates with last thread. But I need to re-write thing. I start over a little. I am stuck now and need your help. Here is my script- #! /bin/ksh export ORACLE_HOME=/opt/oracle/app/oracle/product/9.2 /opt/oracle/app/oracle/product/9.2/bin/sqlplus -s... (5 Replies)
Discussion started by: thepurple
5 Replies

8. Shell Programming and Scripting

redirecting sql query output to a file

Hi, I am executing sql files in my unix shell script. Now i want to find whether its a success or a failure record and redirect the success or failure to the respective files. meaning. success records to success.log file failure record to failure.log file. As of now i am doing like... (1 Reply)
Discussion started by: sailaja_80
1 Replies

9. Shell Programming and Scripting

need help in reading a output of a sql query in unix script

i'm used a sql query in a unix script to get the information from table. but unable to extract the output which i need. Any help with logic will be greatly appreciated. my sql query provide output some thing like this - col1 col2 count ---- ---- ------ A B 10 c D 6 e... (8 Replies)
Discussion started by: pharos467
8 Replies

10. Shell Programming and Scripting

How to store the sql query's output in a variable

Hi, My requirement is : We are calling an sql statement from a UNIX session, and fetching data into some variables from a table .. now we are unable to access these variables from outside the SQL part. Please let me know how can I achieve this. Can you please share a code snippet which... (4 Replies)
Discussion started by: venkatesh_sasi
4 Replies
Login or Register to Ask a Question