LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] comma-separated output (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-comma-separated-output-4175669819/)

schneidz 02-18-2020 09:33 AM

[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


Turbocapitalist 02-18-2020 09:37 AM

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.

schneidz 02-18-2020 09:43 AM

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.

Turbocapitalist 02-18-2020 09:45 AM

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.

danielbmartin 02-18-2020 10:26 AM

Consider this ...
Code:

ls -1 |paste -sd,
Daniel B. Martin

.

Reuti 02-18-2020 12:23 PM

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 -


danielbmartin 02-18-2020 01:04 PM

Quote:

Originally Posted by Reuti (Post 6091468)
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

.

grail 02-18-2020 01:20 PM

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

schneidz 02-18-2020 02:05 PM

Quote:

Originally Posted by Reuti (Post 6091468)
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 (Post 6091485)
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 (Post 6091425)
Consider this ...
Code:

ls -1 |paste -sd,
Daniel B. Martin

.


DGPickett 02-18-2020 08:30 PM

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!

dc.martin 02-18-2020 09:26 PM

$ 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,

astrogeek 02-18-2020 09:32 PM

Quote:

Originally Posted by dc.martin (Post 6091648)
$ 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).

dc.martin 02-18-2020 09:56 PM

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

crts 02-18-2020 10:03 PM

Quote:

Originally Posted by schneidz (Post 6091512)
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.

schneidz 02-19-2020 05:38 AM

Quote:

Originally Posted by crts (Post 6091663)
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/ )


All times are GMT -5. The time now is 11:47 AM.