LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 09: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:	23
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, 10:55 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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 03-31-2013, 11:17 PM   #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:	26
Size:	1.8 KB
ID:	12176Hi,I want it to look like this,thank you for your help
 
Old 03-31-2013, 11:43 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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, 12:04 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

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 12:06 AM.
 
Old 04-01-2013, 12: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, 01:33 AM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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 03:48 AM. Reason: s/Will/Wim/
 
Old 04-01-2013, 03: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



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 5 06-14-2019 08:18 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12: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 07:57 AM
python how to work with fake csv donnied Programming 2 09-29-2008 06:05 AM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:53 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
Open Source Consulting | Domain Registration