LinuxQuestions.org
Register a domain and help support LQ
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 10-29-2008, 04:59 AM   #16
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15

I have used tar czf but its returning an error saying that tar is an unquoted string;

Please check this once.

Thanks,
Prakash.
 
Old 10-29-2008, 01:04 PM   #17
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
How can we check it w/o the code-snippet that does it?
/me isn't clairvoyant - don't know about the other folks on the thread, of course...


Cheers,
Tink
 
Old 10-29-2008, 08:24 PM   #18
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Agree with Tinkster.

Also, there's no point in tarring a single file. You could gzip though if you want to save diskspace.
 
Old 10-30-2008, 02:33 AM   #19
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Thumbs up How to redirect the output of a file to a csv file

Hi,


I have written a perl script which brings data from the database and writes to a text file.

This is the code I used to write to the text file.

open (FH, ">", "/root/Name.txt") or die "Can't open /root/Name.txt: $!\n";

while(my $result = $sth->fetchrow_hashref()){
print FH $result->{'Field1'}."\t";
print FH $result->{'Field2'}."\t";
print FH $result->{'Field3'}."\n";

}
close (FH) or die "Can't close /root/Name.txt: $!\n";

This worked fine.

But now I want to write the data into a CSV file instead of a text file. Can I do this.

Please help me to do this.

Thanks,
Prakash.
 
Old 10-30-2008, 02:46 AM   #20
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,
I have tried it by keeping Name.csv instead of Name.txt.

The csv file is created but all the three fields appeared in the single block. How can we separate those 3 fields and place them in three different blocks.

One more doubt is how can we give headings or titles for those blocks.

Thanks,
Prakash.
 
Old 10-30-2008, 02:47 AM   #21
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
I also never met Miss Voyant
 
Old 10-30-2008, 02:50 AM   #22
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,
Thanks for your replies. What i want to do is just compress the file and attach it to a mail.


Prakash.
 
Old 10-30-2008, 02:52 AM   #23
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Now I have changed my mind and was trying to redirect the data into a csv file instead of the text file.

Please look into the thread
http://www.linuxquestions.org/questi...v-file-679944/

Thanks,
Prakash.
 
Old 10-30-2008, 04:06 AM   #24
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Your fields are seperated by tabs "\t"

If you want a comma seperated file you need to seperate by commas.

Place your field Headings before the while loop.

Modified version of your code:
Code:
open (FH, ">", "/root/Name.csv") or die "Can't open /root/Name.csv: $!\n";

print FH "Field 1,Field 2,Field 3\n";

while(my $result = $sth->fetchrow_hashref()){
    print FH $result->{'Field1'}.",";
    print FH $result->{'Field2'}.",";
    print FH $result->{'Field3'}."\n";

}
close (FH) or die "Can't close /root/Name.txt: $!\n";
 
Old 10-30-2008, 04:13 AM   #25
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
I have posted a reply to your other thread.

In future, please do not start new threads when your half way through getting a problem resolved.
 
Old 10-30-2008, 04:20 AM   #26
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,

Thanks for your help. It worked. I have one more doubt here. How to set the font and change the color for those Titles ( Field1, Field2,Field3);

Thanks,
Prakash.
 
Old 10-30-2008, 04:22 AM   #27
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Sure. I have checked it. Thanks for your reply.

Prakash.
 
Old 10-30-2008, 04:29 AM   #28
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
CSV files are pure text files, you don't change fonts in them.

What are you going to do with this file, is it going to be opened in a spreadsheet application?

If so, import the CSV file into your chosen application, and then use that application to format to your hearts content (you could probably write a macro to do this).
 
Old 10-30-2008, 01:28 PM   #29
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And I have merged your two threads and would ask
you not to do this (two threads - same topic) again,
double-posting is against our rules.



Cheers,
Tink
 
  


Reply


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
How to redirect to a file the output of a service? rtmex Linux - Newbie 5 04-27-2007 12:09 AM
output redirect back to file mathfeel Linux - Software 1 03-21-2007 01:28 PM
How to redirect output to a file? mus1402 Linux - Newbie 2 02-05-2006 10:42 AM
redirect screen output to file timbuck Linux - Software 5 12-09-2005 07:57 PM
output from cpio won't redirect to file rawii Programming 5 01-27-2004 02:49 PM


All times are GMT -5. The time now is 09:10 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration