Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a Shell script with Multiple SQL statements like this:

select Count(*) from Emp where date= current_date-1;

After executing the Shell script i need a MS excel file to be create with Column name (as shown below) and result in my local system and also it has to be mailed to my mail id.

Ms Excel File:

X(Table name)  - Eg: Emp
Result set           120

Can anyone help me on this ??

share|improve this question
    
MS Excel output Should be like Column1- Columnname( Table name mentioned in Select query) in first line and result set in sexond line –  user78873 Jul 28 '14 at 3:44

2 Answers 2

Change the sql statement to contain table name as well

sqlplus /@ < output.dat SET PAGES 0 select 'Emp,' || Count(*) from Emp where date= current_date-1; EXIT; !

awk -F "," '{a[++n] = $1; b[n] = $2; next} END {printf "Table Name"; for(i=1; i<=n; i++) printf ",%s", a[i]; print""; printf "Result Set"; for(i=1; i<=n; i++) printf ",%s", b[i]; print ""}' output.dat > output.csv

uuencode output.csv output.csv | mail -s "Query result" "[email protected]"

PS: I am having hard time compiling code tags for the above script...can someone help me. If I use pre & code tags, the < & > symbols in the code disappears

share|improve this answer
    
You need to replace < and > with HTML entities: &lt; and &gt; –  Hannu Jul 28 '14 at 12:20

Try this example

awk 'BEGIN{ OFS="|"; print "Column1|Column2|Column3|Column4|Column5|Column6"};
     NR > 1{print "IND", "INR", $6, $7, $8, $9;}' Inputdata.txt > Output.xls
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.