mindreader
mindreader

Reputation: 1812

Process substitution working in bash but not is zsh

I'm trying to feed to a while loop using process substitution.

logFile=$1
declare -A frequencyMap

while read line
do

 name=$(echo $line | awk '{print $11}')

 if [ ${frequencyMap[$name]+_} ]

   then frequencyMap[$name]=$(expr ${frequencyMap[$name]} + 1)

   else frequencyMap[$name]=1

 fi

done < <(zgrep 'PersonalDetails' $logFile)

for key in "${!frequencyMap[@]}"
do 
 echo "$key ${frequencyMap[$key]}"
done | sort -rn -k2

This works fine in bash but not in zsh. I'm getting this error:

test.sh: line 20: syntax error near unexpected token `<'
test.sh: line 20: `done < <(zgrep 'Exception' $1)'

I'm using zsh 4.3.10.

How do I get this to work in zsh?

UPDATE: have modified the question to share the complete code

Upvotes: 2

Views: 632

Answers (1)

John Zwinck
John Zwinck

Reputation: 249293

You can use the much more common pipe for this, instead:

zgrep 'PersonalDetails' "$logFile" |
{
  while read line
  do
    # ...
  done

  for key in "${(k)frequencyMap[@]}"
  do 
    echo "$key ${frequencyMap[$key]}"
  done | sort -rn -k2
}

Not only is this more portable, I consider it more readable because the flow starts with opening the input file.

Thanks to @user000001 for pointing out the fact that braces must be used to compose the commands so frequencyMap is visible outside the while loop. For why that's necessary, see here: http://mywiki.wooledge.org/BashFAQ/024

Upvotes: 4

Related Questions