Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
shell scripting to export the results to an excel file

Hi,

I am facing issues while exporting the results retrieved from the Database to an excel file.

I have 5 count query in order to count the records in 5 different tables.

I had connected to the DB and retrieved the column count and printed in the screen.

But wat I need to do is, I have to export the count results to an excel sheet which should be saved in my local system.
The format of the output should be:
Column1 Column2
AAAA 10
BBBB 12
CCCC 100

I searched in other threads and it was given as to export all these to a csv file first and then to excel file. I have also exported to csv file but the o/p is not formatted.

It is like AAAA 10 BBBB 12 CCCC 100

Please help!!!
Sponsored Links
    #2  
Old 02-07-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
How are you printing the results out?
Can you please paste the code you are using?

--ahamed
Sponsored Links
    #3  
Old 02-07-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
Run the output through xargs first:


Code:
$ echo "AAAA 10 BBBB 12 CCCC 100" | xargs -n2
AAAA 10
BBBB 12
CCCC 100



Code:
$ echo "AAAA 10 BBBB 12 CCCC 100" | xargs -n2 | tr ' ' ,
AAAA,10
BBBB,12
CCCC,100

    #4  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Here is my coding:

export ORACLE_SID=ABCD
result=`sqlplus -s /NOLOG << EOF
connect username/pass
SELECT count(*) from A;
`

The same way, am collectng all the 5 query's results in 5 different variables and finally printing on the screen using the print statement. Till this it works perfectly..
result_set=`printf "A: %d\n" $result
printf "B: %d\n" $result1
printf "C %d\n" $result2
printf "D: %d\n" $result3
printf "E: %d\n" $result4

`
Now am collectng altogether in result_set (which I think is unwanted) and then exporting this to csv file.
echo $result_set > results.csv

here it prints everythng in a single line...
Sponsored Links
    #5  
Old 02-07-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Try -e with echo

Code:
echo -e $result_set > results.csv

--ahamed
Sponsored Links
    #6  
Old 02-07-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
Try:

Code:
echo "$result_set" > results.csv

-or-

Code:
printf "%s\n" "$result_set" > results.csv

The Following User Says Thank You to Scrutinizer For This Useful Post:
Learner 20 (03-06-2012)
Sponsored Links
    #7  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
shell scripting to export the results to an excel file

Thanks Scrutinizer & ahamed101,

Both (i)printf "%s\n" "$result_set" > results.csv
(ii) echo "$result_set" > results.csv

is working fine..

Now the output is formatted in the csv file...

Now I need the same to be exported in to an excel file which should be saved in the local PC..

Please give me some idea on that as I am unable to get it from any of the threads...
Sponsored Links
Reply

Tags
exporting the output to an excel file

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
generating report in Excel(Open office) using shell scripting shubham8787 Shell Programming and Scripting 1 06-02-2011 04:26 PM
Creating a file to export into Excel Nacnud OS X (Apple) 2 03-14-2011 09:53 PM
Export SQL results to .TXT file for emailing alpinescott Shell Programming and Scripting 2 08-24-2010 02:19 PM
Need to convert the content of file into COLUMN (To export into excel) velocitnitin Shell Programming and Scripting 6 12-07-2009 12:26 AM
Export to Microsoft excel using shell script goutam_igate Shell Programming and Scripting 1 04-16-2009 06:14 PM



All times are GMT -4. The time now is 04:37 AM.