|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
>> 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 |
|
![]() |
| Outils de la discussion | |
|
|