LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2020, 09:33 AM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
[bash] comma-separated output


hi, how does one create a comma-separated output. this doesnt work as intended ?:
Code:
[schneidz@test]$ ls -1 | tr '\n' ' ' | awk -v OFS=',' '{print $0}'
0.lst 10.lst 11.lst 12.lst 13.lst 14.lst 15.lst 16.lst 17.lst 18.lst 19.lst 1.lst 20.lst 21.lst 22.lst 23.lst 24.lst 25.lst 2.lst 3.lst 4.lst 5.lst 6.lst 7.lst 8.lst 9.lst
 
Old 02-18-2020, 09:37 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,378
Blog Entries: 3

Rep: Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772
Here is one way to do it. In AWK you need to reassign a field to get the whole line reprocessed. That's why this one has $1=$1

Code:
ls -1 \
| tr '\n' ' ' \
| awk '1{$1=$1; print}' OFS=','
The 1 is, of course, always true so the clause always gets run for any given line. But it is probably not necessary.
 
3 members found this post helpful.
Old 02-18-2020, 09:43 AM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
for some reason, this worx:
Code:
[schneidz@test]$ ls -1 | tr '\n' ' ' | awk 'BEGIN{OFS=","}{$1=$1; print}'
0.lst,10.lst,11.lst,12.lst,13.lst,14.lst,15.lst,16.lst,17.lst,18.lst,19.lst,1.lst,20.lst,21.lst,22.lst,23.lst,24.lst,25.lst,2.lst,3.lst,4.lst,5.lst,6.lst,7.lst,8.lst,9.lst
^jinx: thanks for the explanation.

Last edited by schneidz; 02-18-2020 at 09:44 AM. Reason: jinx
 
1 members found this post helpful.
Old 02-18-2020, 09:45 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,378
Blog Entries: 3

Rep: Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772Reputation: 3772
That's more or less the same. The BEGIN clause is as good a place as any to set the Output Field Separator (OFS). And as mentioned the 1 by itself is not needed.
 
Old 02-18-2020, 10:26 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Consider this ...
Code:
ls -1 |paste -sd,
Daniel B. Martin

.
 
6 members found this post helpful.
Old 02-18-2020, 12:23 PM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
I don’t know whether the ls command is the final one or just an example, as the command itself has an option which might work depending on the workflow:
PHP Code:
ls -
 
4 members found this post helpful.
Old 02-18-2020, 01:04 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Reuti View Post
PHP Code:
ls -
This is a winner for being concise. However, it introduces blanks. Based on earlier posts in this thread it appeared that blanks are not wanted. OP, what say you?

Daniel B. Martin

.
 
Old 02-18-2020, 01:20 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well awk brevity would be:
Code:
ls -1 | tr '\n' ' ' | awk '$1=$1' OFS=","
There is an alternative but it still need a sed unfortunately to finish it off, unless you store it all in a variable:
Code:
# with sed
ls -1 | awk 'ORS=","' | sed 's/,$/\n/'

# as a variable
var=$(ls -1 | awk 'ORS=","')
echo "${var%,}"
Daniel's solution seems the briefest, if that is the requirement
 
1 members found this post helpful.
Old 02-18-2020, 02:05 PM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by Reuti View Post
I don’t know whether the ls command is the final one or just an example, as the command itself has an option which might work depending on the workflow:
PHP Code:
ls -
Quote:
Originally Posted by danielbmartin View Post
This is a winner for being concise. However, it introduces blanks. Based on earlier posts in this thread it appeared that blanks are not wanted. OP, what say you?

Daniel B. Martin

.
the ls was just a quick example so i needed something that worx generically. but its interesting nonetheless; however, the paste seems most intriguing:
Quote:
Originally Posted by danielbmartin View Post
Consider this ...
Code:
ls -1 |paste -sd,
Daniel B. Martin

.

Last edited by schneidz; 02-18-2020 at 02:10 PM.
 
Old 02-18-2020, 08:30 PM   #10
DGPickett
LQ Newbie
 
Registered: Jan 2012
Posts: 17

Rep: Reputation: Disabled
In any language, even she’ll, csv is easy, 4 rules:
1. Put commas between fields
2 put cr-lf between rows
3. If any of them are in your field, enclose in double quotes “”.
4. For a literal double quote, double it, like: “He said “”Hello!”””
Enjoy!
 
Old 02-18-2020, 09:26 PM   #11
dc.martin
LQ Newbie
 
Registered: Jan 2017
Posts: 3

Rep: Reputation: Disabled
$ ls -l | tr -s " " ","
total,20
-rw-------,1,root,root,457,Mar,14,2010,cifstab
-rw-r--r--,1,root,root,198,Feb,18,11:34,dhcp.conf
-rw-r--r--,1,root,root,249,Apr,19,2016,lmhosts
-rw-r--r--,1,root,root,1469,Mar,1,2016,smb.conf
-rw-r--r--,1,root,root,379,Apr,19,2016,smbusers

And if you need to get rid of the newlines:


ls -l | tr '\n' "," | tr -s " " ","
total,20,-rw-------,1,root,root,457,Mar,14,2010,cifstab,-rw-r--r--,1,root,root,198,Feb,18,11:34,dhcp.conf,-rw-r--r--,1,root,root,249,Apr,19,2016,lmhosts,-rw-r--r--,1,root,root,1469,Mar,1,2016,smb.conf,-rw-r--r--,1,root,root,379,Apr,19,2016,smbusers,
 
Old 02-18-2020, 09:32 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,276
Blog Entries: 24

Rep: Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224
Quote:
Originally Posted by dc.martin View Post
$ ls -l | tr -s " " ","
total,20
-rw-------,1,root,root,457,Mar,14,2010,cifstab
...
I think you misunderstood the original question. The desired list is ls -1 (ell ess dash one), not ls -l (ell ess dash ell).
 
1 members found this post helpful.
Old 02-18-2020, 09:56 PM   #13
dc.martin
LQ Newbie
 
Registered: Jan 2017
Posts: 3

Rep: Reputation: Disabled
D'OH!

I did misunderstand.

I agree with "ls -m"

If OP doesn't want spaces, then:

$ echo * | tr " " ,
cifstab,dhcp.conf,lmhosts,smb.conf,smbusers
 
2 members found this post helpful.
Old 02-18-2020, 10:03 PM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by schneidz View Post
the ls was just a quick example so i needed something that worx generically. but its interesting nonetheless; however, the paste seems most intriguing:
My favourites are the solutions from post #5 and post#6 so far, nice and short. I do not know exactly what you mean by 'generially', but if you do not mind a trailing comma then here is another alternative:
Code:
printf "%s," *
or maybe
Code:
printf "%s," path/to/elsewhere/*
If the trailing comma is an issue just remove it, e.g., by piping it to sed.
 
2 members found this post helpful.
Old 02-19-2020, 05:38 AM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by crts View Post
My favourites are the solutions from post #5 and post#6 so far, nice and short. I do not know exactly what you mean by 'generially', but if you do not mind a trailing comma then here is another alternative:
Code:
printf "%s," *
or maybe
Code:
printf "%s," path/to/elsewhere/*
If the trailing comma is an issue just remove it, e.g., by piping it to sed.
db2 very much does mind a trailing comma:
https://www.linuxquestions.org/quest...ds-4175669835/

i am parsing up a list of claim-#'s to send to the mainframe in a query.

(a little bit of history:
https://www.linuxquestions.org/quest...ns-4175663736/ )

Last edited by schneidz; 02-19-2020 at 05:46 AM.
 
  


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
want to read multiple csv files in directory and output contents in comma separated form bhartid Linux - Newbie 13 05-23-2018 02:42 PM
[SOLVED] Store array output to comma separated list in bash scripting Rock26 Linux - Newbie 1 07-27-2016 10:26 PM
[SOLVED] Bash script to merge files together (given as a comma separated string) DomeKor Linux - Newbie 10 09-27-2011 11:29 PM
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
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 10:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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