From: Bernd Eggink <eggink@rrz.uni-hamburg.de>
To: Sweth Chandramouli <sweth@astaroth.nit.gwu.edu>
Cc: zsh-users@math.gatech.edu
Subject: Re: related question
Date: Thu, 22 Jan 1998 10:24:42 +0100 [thread overview]
Message-ID: <34C7105A.8331CBA4@rrz.uni-hamburg.de> (raw)
In-Reply-To: <19980121225425.23392@astaroth.nit.gwu.edu>
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@rrz.uni-hamburg.de
http://www.rrz.uni-hamburg.de/eggink/BEggink.html
next prev parent reply other threads:[~1998-01-22 9:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-01-15 0:12 I've forgotten again how to do this --> 0-100 Timothy J Luoma
1998-01-15 0:46 ` Danek Duvall
1998-01-15 1:03 ` Mirar
1998-01-15 10:28 ` Andrew Main
1998-01-15 15:01 ` Geoff Wing
1998-01-15 17:39 ` Brandon C. George
1998-01-15 17:51 ` Andrew Main
1998-01-15 20:32 ` Sweth Chandramouli
1998-01-15 22:59 ` Geoff Wing
1998-01-21 16:26 ` loops and all that Sven Guckes
1998-01-22 3:54 ` related question Sweth Chandramouli
1998-01-22 9:24 ` Bernd Eggink [this message]
1998-01-22 13:00 ` Mirar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=34C7105A.8331CBA4@rrz.uni-hamburg.de \
--to=eggink@rrz.uni-hamburg.de \
--cc=sweth@astaroth.nit.gwu.edu \
--cc=zsh-users@math.gatech.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).