[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}' |
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 \ |
for some reason, this worx:
Code:
[schneidz@test]$ ls -1 | tr '\n' ' ' | awk 'BEGIN{OFS=","}{$1=$1; print}' |
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.
|
Consider this ...
Code:
ls -1 |paste -sd, . |
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:
|
Quote:
Daniel B. Martin . |
Well awk brevity would be:
Code:
ls -1 | tr '\n' ' ' | awk '$1=$1' OFS="," Code:
# with sed |
Quote:
Quote:
Quote:
|
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! |
$ 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, |
Quote:
|
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 |
Quote:
Code:
printf "%s," * Code:
printf "%s," path/to/elsewhere/* |
Quote:
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. |