LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-19-2020, 07:32 AM   #16
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

xsv is a useful tool for CSV handling.

I've not used it in a while, but this (untested) might be another option:
Code:
input | xsv fmt --delimiter=' '
I've only glanced at the diff thread, but perhaps xsv's select or join options could help simplify some of that logic.

Although I I've just seen you mention AIX, so not sure if it's a viable option.

 
Old 02-19-2020, 08:21 AM   #17
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by boughtonp View Post
xsv is a useful tool for CSV handling.

I've not used it in a while, but this (untested) might be another option:
Code:
input | xsv fmt --delimiter=' '
I've only glanced at the diff thread, but perhaps xsv's select or join options could help simplify some of that logic.

Although I I've just seen you mention AIX, so not sure if it's a viable option.
I participated in the thread that OP linked and if I remember correctly then OP is not allowed to install any 3rd party tools at all.

Quote:
Originally Posted by schneidz View Post
Well, I guess then you can still not provide some more representative sample data, right? I am still unclear what you mean by more general solution. Do you want to read the data from a file?
 
Old 02-19-2020, 09:34 AM   #18
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
I participated in the thread that OP linked and if I remember correctly then OP is not allowed to install any 3rd party tools at all.



Well, I guess then you can still not provide some more representative sample data, right? I am still unclear what you mean by more general solution. Do you want to read the data from a file?
the confusion was whether i was specifically looking for a c.s.v. output for ls. i was just using it as an example therefore ls -m doesnt help. i needed something that puts commas in place of any white-space or new-lines between strings for any generic comnand from piped input (without me having to worry about preceding or trailing commas which mash up sql in() clause.)

Last edited by schneidz; 02-19-2020 at 09:37 AM.
 
1 members found this post helpful.
Old 02-19-2020, 10:08 AM   #19
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by schneidz View Post
i needed something that puts commas in place of any white-space or new-lines between strings for any generic comnand from piped input
Well whitespace is a group of characters which includes newline characters.

If you only care about space and newline the existing paste solution with tr seems simplest:
Code:
input | tr ' ' '\n' | paste -sd,
Otherwise, if AIX sed supports the -z argument, you can do it in a single sed call...

Replacing space (ascii 32) or newline (ascii 10) with a comma, then removing trailing comma:
Code:
input | sed -z 's/[\n ]/,/g; s/,$//'
Same thing, but replacing all sequential whitespace characters (ascii 9,10,11,13,32) with a comma:
Code:
input | sed -rz 's/\s+/,/g; s/,$//'
 
Old 02-19-2020, 04:33 PM   #20
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,808

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The following join function works in bash, ksh, zsh:
Code:
join(){
typeset delim=$1 tmpstr
shift || return
tmpstr=$(printf ",%s" "$@")
printf "%s\n" "${tmpstr#$delim}"
}
Now run it as
Code:
join "," *
 
Old 02-19-2020, 04:54 PM   #21
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MadeInGermany View Post
The following join function works in bash, ksh, zsh:
Code:
join(){
typeset delim=$1 tmpstr
shift || return
tmpstr=$(printf ",%s" "$@")
printf "%s\n" "${tmpstr#$delim}"
}
Now run it as
Code:
join "," *
If you want to use a variable, may I suggest a simple echo to avoid the parsing overhead of the second printf?

Code:
join() {
        typeset delim=$1 tmpstr
        shift || return
        printf -v tmpstr "$delim%s" "$@"
        echo "${tmpstr#$delim}"
}
It works in Bash, do not know about ksh and zsh since I do not use them.
 
1 members found this post helpful.
Old 02-20-2020, 03:34 AM   #22
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,808

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Good point.
The -v is more efficient because it does not require a fork().
But it is bash-only.

BTW for robust functions I use printf not echo.
For example:
Code:
join "," -n
will not print the -n if echo interprets it as an option.

Last edited by MadeInGermany; 02-20-2020 at 03:38 AM.
 
1 members found this post helpful.
Old 02-22-2020, 06:55 AM   #23
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by MadeInGermany View Post
BTW for robust functions I use printf not echo.
For example:
Code:
join "," -n
I always seem to forget about that. This problem has never backfired on and I have only encountered it when someone pointed it out. Maybe I should give more consideration to 'printf' in the future, but I guess bad habits die hard :)
 
Old 02-23-2020, 08:51 AM   #24
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I always prefer to use shell built-ins as much as possible. And you can do a lot with them.

In this case, since we're dealing with lists of filenames, the go-to tools are arrays and globbing (assuming bash or similar shell that supports them).

Code:
$ flist=( * )                # note that the sort order may differ from ls -1
$ clist=$( IFS=, ; echo "${flist[*]}" )
$ echo "$clist"

or:

$ printf -v clist '%s,' *    #no need to bother with an array with printf
$ clist="${clist%,}"
$ echo "$clist"
For the first option, an array variable with * as its index expands to all of its elements, separated by the first character of IFS.

Unfortunately, due to the parsing sequence, you have to set IFS first as a separate command, before variable expansion. So in this case I have opted to expand the result in a subshell and transfer the output to a new variable, to avoid having the modified IFS affect the main shell.

The second option uses the printf idea from earlier, except that the superfluous comma is handled by again transferring the resulting string to a second variable, and using a simple parameter substitution to remove it.

edit1: Doh, I missed that MadeInGermany has already given this solution.

edit2: Just a heads-up, "join" is already the name of a coreutil application, albeit one with a completely different function.

Last edited by David the H.; 02-23-2020 at 08:57 AM.
 
1 members found this post helpful.
Old 02-23-2020, 12:44 PM   #25
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,808

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You are right, a join utility exists! Did not think of it.
Suggested new unique name for my function: prtjoin
 
  


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 11:58 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