Unix Programming - how to concatenate several fields in comma delimited file.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2006 > how to concatenate several fields in comma delimited file.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to concatenate several fields in comma delimited file.
Ryan

2006-02-26, 10:16 am

I need to get a list of files from a directory and then concantenate
them into one variable with comma seperating them.

With higher level languages I just do something like this

loop
myVar := myVar (concantenate)',' (concatenate) <new value>

how do i loop through a directory and do this with a list of files?

Gianni Mariani

2006-02-26, 10:16 am

Ryan wrote:
> I need to get a list of files from a directory and then concantenate
> them into one variable with comma seperating them.
>
> With higher level languages I just do something like this
>
> loop
> myVar := myVar (concantenate)',' (concatenate) <new value>
>
> how do i loop through a directory and do this with a list of files?
>


Which language ?

echo * | sed -e "s/ /,/g"
ltordsen

2006-02-26, 10:16 am

Yea, like Gianni said, you can just do this:
#!/bin/sh
list=`ls | sed -e "s/ /,/g"`
echo $list

Måns Rullgård

2006-02-26, 10:16 am

Gianni Mariani <gi2nospam@mariani.ws> writes:

> Ryan wrote:
>
> Which language ?
>
> echo * | sed -e "s/ /,/g"


That won't work if filenames contain spaces. In bash you could do this:

(IFS=,; set -- *; echo "$*")

--
Måns Rullgård
mru@inprovide.com
Chris F.A. Johnson

2006-02-26, 10:16 am

On 2006-02-24, Ryan wrote:
> I need to get a list of files from a directory and then concantenate
> them into one variable with comma seperating them.
>
> With higher level languages I just do something like this
>
> loop
> myVar := myVar (concantenate)',' (concatenate) <new value>
>
> how do i loop through a directory and do this with a list of files?


No loop needed:

list=$( printf "%s," * )
list=${list%,}


Or:

set -f
set -- *
IFS=,
list=$*


If you prefer a loop you can use one (it will be slower on a large
list):

for file in *
do
list=${list:+"$list,"}$file
done

Or:

list=$(
for file in *
do
printf "%s" ${list:+,}$file
done
)


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Logan Shaw

2006-02-26, 8:48 pm

Måns Rullgård wrote:
> Gianni Mariani <gi2nospam@mariani.ws> writes:
>
>
> That won't work if filenames contain spaces. In bash you could do this:
>
> (IFS=,; set -- *; echo "$*")


Or in Perl:

perl -le 'print join ",", <*>'

That also works with spaces in the filenames. However, neither solution
will work well if there are commas in the filenames. (RCS, anyone?)

- Logan
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com