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 > zsh array assignment
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

zsh array assignment

Réponse
 
LinkBack Outils de la discussion
Vieux 23/08/2006, 16h03   #1
Eric Moors
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut zsh array assignment

Hi,

I am trying to use the associative arrays in zsh, but I cannot figure out
how I can assign the output of a command to them. Hopefully someone here
can provide some assistance (or point me to an alternative)

I'd like to put the output of the jhead command (an image EXIF tag parser)
into an array. The output of jhead is as follows:

$ jhead img.jpg| grep "^File"
File name : img.jpg
File size : 2844547 bytes
File date : 2006:08:23 16:48:37

So I pipe it to a sed command:

$ jhead img.jpg| grep "^File"|
sed -n 's/^\([^:]\+[^ ]\)[ ]*: \(.*\)$/\1 \2/;H;${x;s/\n/ /g}'
File name img.jpg File size 2844547 bytes File date 2006:08:23 16:48:37

in order to do this:

$ typeset -A ARRAY
$ ARRAY=(`jhead img.jpg| grep "^File"|
sed -n 's/^\([^:]\+[^ ]\)[ ]*: \(.*\)$/\1 \2/;H;${x;s/\n/ /g}'`)
zsh: bad set of key/value pairs for associative array

So I tried several ways of quoting \1 and \2 (' as well as ")

$ jhead img.jpg| grep "^File"|
sed -n 's/^\([^:]\+[^ ]\)[ ]*: \(.*\)$/"\1" "\2"/;H;${x;s/\n/ /g}'
"File name" "img.jpg" "File size" "2844547 bytes" "File date" "2006:08:23
16:48:37"

$ jhead img.jpg| grep "^File"| sed -n 's/^\([^:]\+[^ ]\)[ ]*:
\(.*\)$/'\''\1'\'' '\''\2'\''/;H;${x;s/\n/ /g}'
'File name' 'img.jpg' 'File size' '2844547 bytes' 'File date' '2006:08:23
16:48:37'

but to no avail, it keeps complaining!

Any would be greatly appreciated.

Eric


  Réponse avec citation
Vieux 23/08/2006, 16h36   #2
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: zsh array assignment

On Wed, 23 Aug 2006 17:03:14 +0200, Eric Moors wrote:
> Hi,
>
> I am trying to use the associative arrays in zsh, but I cannot figure out
> how I can assign the output of a command to them. Hopefully someone here
> can provide some assistance (or point me to an alternative)
>
> I'd like to put the output of the jhead command (an image EXIF tag parser)
> into an array. The output of jhead is as follows:
>
> $ jhead img.jpg| grep "^File"
> File name : img.jpg
> File size : 2844547 bytes
> File date : 2006:08:23 16:48:37

[...]

Something like:

typeset -A a
jhead img.jpg | while IFS=": " read -r k v; do a[$k]=$v; done

or:

typeset -A a
IFS=$'\n\n'
a=(
$(jhead img.jpg | sed -n 's/ *: */\
/p')
)


(for example).

--
Stephane
  Réponse avec citation
Vieux 24/08/2006, 07h09   #3
Eric Moors
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: zsh array assignment

>> I am trying to use the associative arrays in zsh, but I cannot figure out
>> how I can assign the output of a command to them. Hopefully someone here
>> can provide some assistance (or point me to an alternative)
>>
>> I'd like to put the output of the jhead command (an image EXIF tag
>> parser) into an array. The output of jhead is as follows:
>>
>> $ jhead img.jpg| grep "^File"
>> File name : img.jpg
>> File size : 2844547 bytes
>> File date : 2006:08:23 16:48:37

> [...]
>
> Something like:
>
> typeset -A a
> jhead img.jpg | while IFS=": " read -r k v; do a[$k]=$v; done


I also realized that I could use a while loop, but I preferred
to do without one. Pasting the output of the jhead|... line in
the array assigment worked fine, so I assumed there had to be some
way of doing it directly.

> or:
>
> typeset -A a
> IFS=$'\n\n'
> a=(
> $(jhead img.jpg | sed -n 's/ *: */\
> /p')
> )
>


Setting IFS!
This is just the hint I needed!

I am now using this.

IFS=$'\n='
typeset -A a
a=($(jhead img.jpg | sed -n 's/^\([^:]\+[^ ]\)[ ]*:[ ]\+\(.*\)$/\1=\2/p'))

Thanks

Eric
  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 19h15.


É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,10690 seconds with 11 queries