LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-29-2016, 12:39 PM   #1
bpfenerty
LQ Newbie
 
Registered: Oct 2015
Posts: 7

Rep: Reputation: Disabled
CLI command to create comma separated string


I am trying to take the contents of one file (fruit) that looks like this:
apples
pears
bananas

and copy that data to another file (fruit2) in one long line separated by commas, with no extra space after text:

apples,pears,bananas

Any help would be much appreciated!

Brian
 
Old 01-29-2016, 12:58 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Show us your work.

https://www.linuxquestions.org/quest...#faq_lqwelcome
 
1 members found this post helpful.
Old 01-29-2016, 01:35 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
You could do that several ways, simple shell script or simple sed expression or awk for something you could more easily extend such as for multiple columns or more selective handling.

Do you have exposure to any of those? If so just think about the problem in the context of your skill set and see where that leads.

For examaple, if shell scripting think about a read loop. Or for sed think multi-line replacement. Or for awk think in terms of records and input/output record separators. Also consider whether you might want them reordered (sorted), allow duplicates, etc.

And to get you started, here is a working sed expression:
Code:
':start; $!{N;b start;};s/\n/,/g'
...and a hint for awk, with a suitable ORS it reduces to a single print $0 operation.

For each of the above cases, a simple file redirect will put it into the output file of your choice.

Last edited by astrogeek; 01-29-2016 at 01:37 PM.
 
Old 01-29-2016, 02:24 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Simpler to use tr eg
Code:
keithhedger@LFSHal:~-> cat /tmp/fruit
apples
pears
bananas
keithhedger@LFSHal:~-> cat /tmp/fruit|tr "\n" ","
apples,pears,bananas,keithhedger@LFSHal:~->
I leave it to the OP to do a litle clean up, redirect to file fruit2, remove the trailing comma and add a new line, I aint doing all his homework!

That's one of the good things about Linux, multiple solutions.
 
Old 01-29-2016, 03:56 PM   #5
bpfenerty
LQ Newbie
 
Registered: Oct 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by astrogeek View Post
You could do that several ways, simple shell script or simple sed expression or awk for something you could more easily extend such as for multiple columns or more selective handling.

Do you have exposure to any of those? If so just think about the problem in the context of your skill set and see where that leads.

For examaple, if shell scripting think about a read loop. Or for sed think multi-line replacement. Or for awk think in terms of records and input/output record separators. Also consider whether you might want them reordered (sorted), allow duplicates, etc.

And to get you started, here is a working sed expression:
Code:
':start; $!{N;b start;};s/\n/,/g'
...and a hint for awk, with a suitable ORS it reduces to a single print $0 operation.

For each of the above cases, a simple file redirect will put it into the output file of your choice.

I am just learning text manipulation, so no I don't have any exposure to awk or sed yet. I'll do a little research on them tonight and see if I can use that to make this work, thanks for your assistance!

Brian
 
Old 01-29-2016, 03:57 PM   #6
bpfenerty
LQ Newbie
 
Registered: Oct 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
Simpler to use tr eg
Code:
keithhedger@LFSHal:~-> cat /tmp/fruit
apples
pears
bananas
keithhedger@LFSHal:~-> cat /tmp/fruit|tr "\n" ","
apples,pears,bananas,keithhedger@LFSHal:~->
I leave it to the OP to do a litle clean up, redirect to file fruit2, remove the trailing comma and add a new line, I aint doing all his homework!

That's one of the good things about Linux, multiple solutions.



Thank you for the point in the right direction!!

Brian
 
Old 01-29-2016, 04:49 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by bpfenerty View Post
I am just learning text manipulation, so no I don't have any exposure to awk or sed yet. I'll do a little research on them tonight and see if I can use that to make this work, thanks for your assistance!
You are welcome!

Good luck in your learning experience!

Sed and awk are the go-to tools for text manipulation on a Unix/Linux system and are well worth learning! Neither is difficult, but can sometimes appear a little obscure on first contact by new users. Just take a deep breath and work a few examples and you'll be using them for literally everything before you know it!

There are many tutorials for both online, a quick DuckDuckGo turned up these which look useful:

* SED Tutorial; Learn Linux SED
* Sed - An Introduction and Tutorial
* And from the same author, Awk - A Tutorial
* A Guided Tour of Awk
* And of course the wikipedia pages for sed, and awk


You'll see that both sed and awk rely on regular expressions, so it is worth learning a little about those as a separate topic (although most sed and awk tutorials will necessarily include the basics).

* This page looks like it has everything you will ever want to know about regular expressions!
* Or the more tutorial, Regular Expressions from Princeton

Those should provide enough examples to get you started, and enough detail if you really want to learn it from the ground up!

So, get yourself oriented and work a few examples, and let us know if you need help past the bumps!

Last edited by astrogeek; 01-29-2016 at 05:08 PM. Reason: typos
 
Old 01-30-2016, 12:38 PM   #8
bpfenerty
LQ Newbie
 
Registered: Oct 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
You are welcome!

Good luck in your learning experience!

Sed and awk are the go-to tools for text manipulation on a Unix/Linux system and are well worth learning! Neither is difficult, but can sometimes appear a little obscure on first contact by new users. Just take a deep breath and work a few examples and you'll be using them for literally everything before you know it!

There are many tutorials for both online, a quick DuckDuckGo turned up these which look useful:

* SED Tutorial; Learn Linux SED
* Sed - An Introduction and Tutorial
* And from the same author, Awk - A Tutorial
* A Guided Tour of Awk
* And of course the wikipedia pages for sed, and awk


You'll see that both sed and awk rely on regular expressions, so it is worth learning a little about those as a separate topic (although most sed and awk tutorials will necessarily include the basics).

* This page looks like it has everything you will ever want to know about regular expressions!
* Or the more tutorial, Regular Expressions from Princeton

Those should provide enough examples to get you started, and enough detail if you really want to learn it from the ground up!

So, get yourself oriented and work a few examples, and let us know if you need help past the bumps!
So I was able to accomplish what I needed for my assignment using the following commands:

cat file | xargs | sed -e 's/ /,/g'

I used that based on stuff I found last night with the wonderful google machine. I also read up on the sed command and understand that portion of it. I am struggling with xargs though. I would not have even used it if I didn't find a similar situation and solution on stack overflow. So I understand cat, but why I am I piping to xargs and what exactly is it? Thanks again for answering some questions!

Brian
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script to merge files together (given as a comma separated string) DomeKor Linux - Newbie 10 09-27-2011 11:29 PM
[SOLVED] Randomize the comma separated string in shell mariakumar Linux - General 13 10-11-2010 12:32 AM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:43 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