PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > for loop issues
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

for loop issues

Réponse
 
LinkBack Outils de la discussion
Vieux 30/08/2007, 19h56   #1
peter sands
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut for loop issues

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

  Réponse avec citation
Vieux 30/08/2007, 20h03   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop issues

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\ntype\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\ntype\t$4\n"} '

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

Ed.
  Réponse avec citation
Vieux 30/08/2007, 22h48   #3
Jeremiah DeWitt Weiner
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop issues

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
  Réponse avec citation
Vieux 30/08/2007, 23h21   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop issues

On 2007-08-30, Jeremiah DeWitt Weiner wrote:
> 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


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
  Réponse avec citation
Vieux 30/08/2007, 23h52   #5
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: for loop issues

Chris F.A. Johnson wrote:
> On 2007-08-30, Jeremiah DeWitt Weiner wrote:
>> 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

>
> 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12762 seconds with 13 queries