Unix Shell - for loop issues

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2007 > for loop issues





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 for loop issues
peter sands

2007-08-30, 7:21 pm

Hi
I am extracting info from files that are held in $container , this
string will always contain 4 values, I now need to generate a report
thru these extractions.

I use the following to print out $container values :

for items in $container
do
echo "$items"
done

This is all OK, but now I need to assign a description to each item,
these will always be in order of:
"container stub weight type"

How do I assoicated the description with the values in a for loop ?
Or how do I do it another way , so I get something like:

container LPSEC
stub 3YB44
weight 432
type DOM


Thanks
Pete

Ed Morton

2007-08-30, 7:21 pm

peter sands wrote:
> Hi
> I am extracting info from files that are held in $container , this
> string will always contain 4 values, I now need to generate a report
> thru these extractions.
>
> I use the following to print out $container values :
>
> for items in $container
> do
> echo "$items"
> done
>
> This is all OK, but now I need to assign a description to each item,
> these will always be in order of:
> "container stub weight type"
>
> How do I assoicated the description with the values in a for loop ?
> Or how do I do it another way , so I get something like:
>
> container LPSEC
> stub 3YB44
> weight 432
> type DOM
>
>
> Thanks
> Pete
>


awk '{printf " container\t$1\nstub\t$2\nweight\t$3\ntyp
e\t$4\n"}' file

would be the right way to get the output you want from a file, or this
if you want to use an intermediate variable for some reason:

echo "$container" |
awk '{printf " container\t$1\nstub\t$2\nweight\t$3\ntyp
e\t$4\n"}'

There's various alternatives. The right one to use depends what the rest
of your script is doing.

Ed.
Jeremiah DeWitt Weiner

2007-08-30, 7:21 pm

peter sands <peter_sands@techemail.com> wrote:
> This is all OK, but now I need to assign a description to each item,
> these will always be in order of:
> "container stub weight type"


If I interpret your situation right, and your data is always
contained in the same four fields in $container, in the same order, you
can do this:

$ set -- $container
$ echo -e "container $1\nstub $2\nweight $3\ntype $4"

The "set --" syntax sets the positional parameters $1 and up. If
the value of $container is changing, you just have to wrap this code in
a loop, like

while read container ; do
set -- $container
echo -e "container $1\nstub $2\nweight $3\ntype $4"
done

--
Oh to have a lodge in some vast wilderness. Where rumors of oppression
and deceit, of unsuccessful and successful wars may never reach me
anymore.
-- William Cowper
Chris F.A. Johnson

2007-08-30, 7:21 pm

On 2007-08-30, Jeremiah DeWitt Weiner wrote:
> peter sands <peter_sands@techemail.com> wrote:
>
> If I interpret your situation right, and your data is always
> contained in the same four fields in $container, in the same order, you
> can do this:
>
> $ set -- $container
> $ echo -e "container $1\nstub $2\nweight $3\ntype $4"
>
> The "set --" syntax sets the positional parameters $1 and up. If
> the value of $container is changing, you just have to wrap this code in
> a loop, like
>
> while read container ; do
> set -- $container
> echo -e "container $1\nstub $2\nweight $3\ntype $4"
> done


Even simpler:

while read container stub weight type ; do
printf "%s\n" "$container" "$stub" "$weight" "$type"
done




--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Michael Tosch

2007-08-30, 7:21 pm

Chris F.A. Johnson wrote:
> On 2007-08-30, Jeremiah DeWitt Weiner wrote:
>
> Even simpler:
>
> while read container stub weight type ; do
> printf "%s\n" "$container" "$stub" "$weight" "$type"


In the above format and with a portable echo command:

echo "
container $container
stub $stub
weight $weight
type $type"

> done
>
>
>
>



--
Michael Tosch @ hp : com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com