LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-31-2013, 10:11 PM   #1
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Rep: Reputation: Disabled
newline not work with my csv


Hi,I have a file,content as below:
a
b
c
d

and I want insert the content into a csv form,and used the
"echo ,,`cat file`, >file.csv"
but the csv form turns out to be as below:
Click image for larger version

Name:	Capture.PNG
Views:	13
Size:	1.2 KB
ID:	12175

Can anyone help to make the newline work,and keep the format as the file shows,not shows in one line.
 
Old 03-31-2013, 11:55 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

what about the following:
Code:
sed 's/$/,/g' < file > file.csv
If that doesn't do what you want (because at this point we can only guess), please be explicit about what you want the output csv file to look like.

Evo2.
 
Old 04-01-2013, 12:17 AM   #3
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,

what about the following:
Code:
sed 's/$/,/g' < file > file.csv
If that doesn't do what you want (because at this point we can only guess), please be explicit about what you want the output csv file to look like.

Evo2.
Click image for larger version

Name:	Capture.PNG
Views:	13
Size:	1.8 KB
ID:	12176Hi,I want it to look like this,thank you for your help
 
Old 04-01-2013, 12:43 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

Quote:
Originally Posted by nicksu View Post
Attachment 12176Hi,I want it to look like this,thank you for your help
I need to see the csv file itself, not how it is rendered in some spread sheet program. Eg what is the output of
Code:
cat --show-nonprinting file.csv
Evo2.
 
Old 04-01-2013, 01:04 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
You need to embed the text in double quotes. The below was tested in a windows environment and shows how you can embed newlines (double quotes highlighted in bold red) as well as commas in csv fields.

csv file
Code:
"a
b
c
d","1,2,3,4"
"x
y
z","0,9,8,7"
The result
Code:
+----+-------------+
| a  | 1,2,3,4     |
| b  |             |
| c  |             |
| d  |             |
+----+-------------+
| x  | 0,9,8,7     |
| y  |             |
| z  |             |
+----+-------------+
Explanation:
unless within double quotes, newlines indicate the end of the records; similar, commas indicated the end of a field

Last edited by Wim Sturkenboom; 04-01-2013 at 01:06 AM.
 
Old 04-01-2013, 01:41 AM   #6
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,


I need to see the csv file itself, not how it is rendered in some spread sheet program. Eg what is the output of
Code:
cat --show-nonprinting file.csv
Evo2.

[ssh@Nick sf_osshare]$ cat --show-nonprinting t.csv
,,a
b
c
d,
HI,nothing different
 
Old 04-01-2013, 02:33 AM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

I thought that Wim's post was the answer...
Quote:
Originally Posted by nicksu View Post
[ssh@Nick sf_osshare]$ cat --show-nonprinting t.csv
,,a
b
c
d,
HI,nothing different
Does that file display as you want it to in your spread sheet? If yes, then what is the problem? If no, then please post the contents of the file that does.

Evo2.

PS. Please use [code] tags where appropriate.

Last edited by evo2; 04-01-2013 at 04:48 AM. Reason: s/Will/Wim/
 
Old 04-01-2013, 04:38 AM   #8
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,

I thought that Will's post was the answer...


Does that file display as you want it to in your spread sheet? If yes, then what is the problem? If no, then please post the contents of the file that does.

Evo2.

PS. Please use [code] tags where appropriate.
Hi,thank you for your help.a printf work,thank you
 
  


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
Sed: insert a newline. Why does not it work? J_Szucs Linux - Software 4 03-19-2014 02:28 PM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 01:45 PM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 08:57 AM
python how to work with fake csv donnied Programming 2 09-29-2008 07:05 AM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 07:11 PM


All times are GMT -5. The time now is 04:06 PM.

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