Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: related question



Sweth Chandramouli wrote:
> 
> while read line ; do
>    for ((i=1;i<=11;i++)) ; do
>       field[$i]=`echo $line | cut -d';' -f$i | tr -d '"'`
>    done
> done < in.file
> 
>         this should take a line of the format
> 
> "foo1";"foo2";"foo3"; [...] ;"foo11"
> 
>         and stuff each foo# into the appropriate spot in the array named field.

You don't need cut and tr, zsh can do all that:

   while read line
   do field=(${(s(;))line})  # split at ;
      field=${field#?}       # remove leading quotes
      field=${field%?}       # remove trailing quotes
   done

> my question is, how do i then concatenate all of these values back into one
> line?  what i'm currently doing, since the max value for i is so small, is just
> 
> echo
> "\"$field[1]\";\"$field[2]\";\"$field[3]\";\"$field[4]\";\"$field[5]\";\"$field[
> 6]\";\"$field[7]\";\"$field[8]\";\"$field[9]\";\"$field[10]\";\"$field[11]\""
> 
> pretty soon, however, i'm going to have to use this script on a larger set of
> data, with a lot more fields per line; i don't want to have to type out each
> item of the array individually.  i know there's an easier way to do it, but it's
> late, and my brain isn't working very well.
> 
>         any suggestions (other than using perl, which is what all of my
> coworkers say to do)?

   for ((i=1; i<=$#field; ++i))
   do 
      field[i]="\"$field[i]\""   # restore quotes
   done

   line=${(j(;))field}   # join elements using ;

Hope that helps!

-- 
Bernd Eggink
Regionales Rechenzentrum der Universitaet Hamburg
eggink@xxxxxxxxxxxxxxxxxx
http://www.rrz.uni-hamburg.de/eggink/BEggink.html



Messages sorted by: Reverse Date, Date, Thread, Author