LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl programming question (https://www.linuxquestions.org/questions/programming-9/perl-programming-question-672265/)

dayalan_cse 09-25-2008 05:29 AM

perl programming question
 
Hello All,

I want to read the information from one file (in unix) and put it in to csv (comma separated values) i need to use perl for this.

I am not sure how multiple lines can be put in to same cell like below.

linux
questions
linux1
questions2

when i tried the multiple lines it shows like below, what i should do to put this information muliple line in a single cell.

linuxquestionslinux1questions2

Please let me know if you have any inputs.

keefaz 09-25-2008 11:08 AM

You want concatenate some lines in one variable ? use the ' . ' operator
Or if you want read multiple line in one pass, one way is unset $/ and use s/\n//g

Telemachos 09-25-2008 11:26 AM

Two to check out: perldoc -f chomp, perldoc -f join

So, for example if you have the lines chomped and saved in an array, you can easily produce a comma-separated group as easy as
Code:

$csv = join ',', @lines;
If you need only to print a report, not save the value
Code:

print join ',', @lines;

dayalan_cse 09-26-2008 09:34 AM

Quote:

Originally Posted by Telemachos (Post 3291529)
Two to check out: perldoc -f chomp, perldoc -f join

So, for example if you have the lines chomped and saved in an array, you can easily produce a comma-separated group as easy as
Code:

$csv = join ',', @lines;
If you need only to print a report, not save the value
Code:

print join ',', @lines;

Hello,

thanks for your inputs.

my question was how to put muliple lines in single cell, means

1) i opened Microsoft excel sheet
2) i selected a column (cell)
3) i typed as linux and pressed ctrl+enter keys, it goes to the next line in the same cell and i type again linux1 and repeating the ctrl+enter steps
4) i saved this as .csv file

when i opened .csv file in notepad it was showing
"linuxlinux1" like this string

i edited this .csv file in notepad, "computer1computer2" and saved it and i opened in excel but i could not see the string in multiple lines in the single cell.

how to do this in .csv file format.

Telemachos 09-26-2008 12:25 PM

Quote:

Originally Posted by dayalan_cse (Post 3292509)
Hello,

thanks for your inputs.

my question was how to put muliple lines in single cell, means

1) i opened Microsoft excel sheet
2) i selected a column (cell)
3) i typed as linux and pressed ctrl+enter keys, it goes to the next line in the same cell and i type again linux1 and repeating the ctrl+enter steps
4) i saved this as .csv file

when i opened .csv file in notepad it was showing
"linuxlinux1" like this string

i edited this .csv file in notepad, "computer1computer2" and saved it and i opened in excel but i could not see the string in multiple lines in the single cell.

how to do this in .csv file format.

What you have here is a question about Excel or maybe a Notepad question. I can't help because I know nothing about Excel or Notepad. I also don't understand any longer why you called this a Perl question. Good luck.

dayalan_cse 09-27-2008 05:40 AM

Quote:

Originally Posted by Telemachos (Post 3292636)
What you have here is a question about Excel or maybe a Notepad question. I can't help because I know nothing about Excel or Notepad. I also don't understand any longer why you called this a Perl question. Good luck.

Hi Telemachos,

This is perl question, i wanted to write a script which will put multiple lines in a single column.

To be precise in the questions, how can i put mulitiple rows in a cell in .csv file format using perl script.

Requirement is below 3 lines show be in single cell one by one line.

line1
line2
line3

Telemachos 09-27-2008 06:32 AM

The functions chomp and join should be helpful for solving that problem in Perl, as I said above. I don't know anything about working with Excel spreadsheeets in Perl (or out of Perl), so I'm not sure about the "in one cell" requirement. I would recommend searching on Google for Perl and Excel and on CPAN for Perl modules for Excel.

chrism01 09-29-2008 01:25 AM

Quote:

multiple lines in a single column.
Quote:

mulitiple rows in a cell
are 2 different things....

What exactly do you want? Give a clear example of before and after.

jschiwal 09-29-2008 01:32 AM

Give a "real" example of before and after.

dayalan_cse 10-06-2008 10:13 PM

Quote:

Originally Posted by jschiwal (Post 3294757)
Give a "real" example of before and after.

Hello All,

thanks for the information.

I found it, to get the multiple lines in single cell in csv file format we need to use \n like below

"linux1\nlinux2\nlinux3\nlinux4"

if we print the above string in to a file it would display the above string line by line in single cell in spreadsheet.

Thanks,
Deenadayalan


All times are GMT -5. The time now is 01:44 PM.