LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-20-2008, 12:12 PM   #1
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Rep: Reputation: 15
Smile Need help on writing data to csv file


Hi,

I want to copy result of two diffrent sybase queries in adjacent cells of the csv file ,say


query 1:select fund_id from table1 where fund_code=123
result1:ABC

query 2:select end_date from table2 where fund_code=123
result2:31-Dec-2008

cell1 | cell2
______ |_____
result1 | result2
_______ |________

I am using following command to send data to csv file.

isql -Sserver -Ppassword -o result.csv

Also please suggest how to append data to csv file for more queries.

Thanks in advance.....

Last edited by tucs_123; 12-20-2008 at 12:18 PM.
 
Old 12-20-2008, 12:51 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
select t1.fund_id || ',' || t2.end_date 
   from table1 t1, table2 t2 
  where t1.fund_code = t2.fund_code and 
        t1.fund_code = '123';
You'll probably want to change the fund_code = '123' to increase your result set.

I think this is what you are asking for.

Last edited by crabboy; 12-20-2008 at 12:52 PM.
 
Old 12-20-2008, 08:18 PM   #3
sec-per
LQ Newbie
 
Registered: Dec 2008
Posts: 2

Rep: Reputation: 0
Probably

Code:
man join
can help
 
Old 12-21-2008, 09:26 AM   #4
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks crabboy for your quick reply...yaa..I think this query will work....But I had given just sample queries.

My actual queries are:


1)select min(start_time) from table1 where req_name='s_20080709' and end_date='6/30/2008'


2)select max(upd_time) from table1 where req_name='s_20080709' and end_date='6/30/2008'




3)select min(start_time) from table1 where req_name='s_20080709' and end_date='7/31/2008'


4)select max(upd_time) from table1 where req_name='s_20080709’ and end_date='7/31/2008'

As you can see table name is same for all the queries but end_date is different for first two and last two queries.

I can combine query1 and query2 to single query and pass result to csv file and can combine query3 and query4 to single query and pass result to csv file.

How can I handle the field end_date,so that I can combine the result of all four queries and pass it to csv file.so that it will look like in csv file as below.

server1: result1 |result2| result3| result4|


Please help on how to append data to csv file as I will be running these quries for different servers.

server1: result1 |result2| result3| result4|
server2: result1 |result2| result3| result4|

Last edited by tucs_123; 12-21-2008 at 09:28 AM.
 
Old 12-21-2008, 11:51 AM   #5
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
You did not specify what database you are using, but for Oracle, something like this will work:
Code:
create or replace procedure test1( condition1 varchar2, condition2 varchar2, condition3 varchar2) as
lv_min varchar2(20);
lv_max varchar2(20);
lv_min2 varchar2(20);
lv_max2 varchar2(20);

BEGIN
   select min(start_time) into lv_min from aps_account where req_name=condition1 and end_date=condition2;
   select max(upd_time) into lv_max from aps_account  where req_name=condition1 and end_date=condition2;
   select min(start_time) into lv_min2 from aps_account where req_name=condition1 and end_date=condition3;
   select max(upd_time) into lv_max2 from aps_account where req_name=condition1 and end_date=condition3;
   
   dbms_output.put_line('Server x: ' || lv_min || '|' || lv_max || '|' || lv_min2 || '|' || lv_max2 );
END;
Execute it like this:
Code:
SQL> execute test1( 's_20080709’, '6/30/2008', '7/31/2008'  );
To execute it from a script:
test.sql
Code:
set serveroutput on;
execute test1( 's_20080709’, '6/30/2008', '7/31/2008'  );
exit
A test script, test.sh:
Code:
OUTFILE=test.out
sqlplus -S dbuser/dbpass@sid @test.sql | grep Results >> $OUTFILE
 
Old 12-21-2008, 11:29 PM   #6
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks a lot crabboy...... I am using Sybase as database as I used command "isql -Sserver -Ppassword -o result.csv" in my first post...As per your reply,got an idea how to proceed further...I will try to use that for Sybase.

Last edited by tucs_123; 12-22-2008 at 12:55 AM.
 
Old 12-23-2008, 12:34 PM   #7
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
Smile

Hi crabboy.....could you please post the solution for Sybase as well in detail as you provided for the oracle like creating procedure,running it,passing parameters to it etc.as I tried to work on that but getting some errors with Sybase procedure....

Last edited by tucs_123; 12-23-2008 at 12:38 PM.
 
Old 12-24-2008, 08:08 AM   #8
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
How to copy result of exec command in sybase to csv or data file

Hi,

I am executing Sybase procedure test3

create procedure test3( @condition1 varchar(20), @condition2 varchar(20), @condition3 varchar(20) ) as

DECLARE
@lv_min varchar(20),
@lv_max varchar(20),
@lv_min2 varchar(20),
@lv_max2 varchar(20)

begin
select @lv_min=(select min(start_time) from tabel1 where requestor_name= @condition1 and end_date= @condition2)
select @lv_max=(select max(upd_time) from tabel1 where requestor_name= @condition1 and end_date= @condition2)
select @lv_min2=(select min(start_time) from tabel1 where requestor_name= @condition1 and end_date= @condition3)
select @lv_max2=(select max(upd_time) from tabel1 where requestor_name= @condition1 and end_date= @condition3)
select @lv_min
select @lv_max
select @lv_min2
select @lv_max2
end


>exec test3 's_20080709’, '6/30/2008', '7/31/2008'

and the results are

------
result1

-------
result2

-------
result3

-------
result4


How to copy these results to csv file,so that it will look like..

cell1 cell2 cell3 cell4
result1|result2|result3|result4

Last edited by tucs_123; 12-24-2008 at 10:29 AM.
 
Old 12-25-2008, 05:35 AM   #9
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
copying data from a text file to csv file

Hi,

Can someone help me in copying data from a text file to csv file using shell script.

Say my text file is hi.txt with following contents:

monday tuesday wedneday

I can use:

cat hi.txt > queue.csv

but the output is not copied properly to csv file.


it should look like:

cell1 | cell2 | cell3
monday | tuesday | wedneday

i.e.first text from text file should go in first cell of csv file,second text in second cell and so on...


Thanks in advance.......

Last edited by tucs_123; 12-25-2008 at 06:13 AM.
 
Old 12-25-2008, 06:28 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Please do not start multiple threads on the same subject. I'm merging the two recent ones into your original thread.

Last edited by pixellany; 12-25-2008 at 06:32 AM.
 
Old 12-28-2008, 06:15 AM   #11
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
Hi,

Can any one tell me how to copy data to a excel/csv file from text file to other columns,leaving first column unaffected i.e it should not overwrite data in first column using shell script.

Say my text file data is:

15-dec-2008 15-dec-2009
16-dec-2008 16-dec-2009


say my first excel column is:

column1 column2 column3
server1
server2


I want output as below:

column1 column2 column3
server1 15-dec-2008 15-dec-2009
server2 16-dec-2008 16-dec-2009

Thanks in advance........

Last edited by tucs_123; 12-28-2008 at 06:16 AM.
 
Old 12-28-2008, 11:19 PM   #12
sec-per
LQ Newbie
 
Registered: Dec 2008
Posts: 2

Rep: Reputation: 0
Hi,

Try this :

1- place the server list in a file (f1.txt)
2- convert the excel file into a space separated file (say f2.txt)


Then type

Code:
paste f1.txt f2.txt > resutl.txt
This will produce a space/tab separated file.

To get a cvs file, type


Code:
paste f1.txt f2.txt  | sed 's/[\t| ]/;/g' > resutl.cvs

Hope it helps.

Last edited by sec-per; 12-28-2008 at 11:30 PM.
 
Old 12-29-2008, 03:42 AM   #13
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
hey thanks buddy...it helped me a lot.....
 
  


Reply

Tags
copy, csv, file, query, result, sql


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript for writing data to file Dyuri Programming 2 10-16-2008 02:55 PM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
looking for a perl script to convert html table data into a csv file swiftguy121 Linux - Software 2 04-25-2007 07:28 PM
help extracting data from csv file willinusf Linux - General 10 10-27-2006 09:10 PM
Culling Data from a CSV file to output in excel jterr02 Programming 2 05-19-2006 04:58 AM


All times are GMT -5. The time now is 05:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration