Can I pipe ispell output to an array?

梦想与她 提交于 2019-12-23 05:01:46

问题


I need to somehow get the output of ispell -l into an array so that I can loop through them..

So far ive got this

cat $1 | ispell -l

I have tried to read them in line by line into an array but that hasnt worked for me

any suggestions?


回答1:


Recent bash comes with mapfile or readarray:

   readarray stuff < <(ispell -l < "$1")
   echo "number of lines: ${#stuff[@]}"

(this example returns effectively ispell -l < "$1"|wc -l)

beware of the mistake to do e.g. ls | readarray, it won't work because readarray would be in a subshell because of the pipe. Use input redirection only.


   mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
   readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
          Read lines from the standard input into the indexed array variable array, or from file descriptor fd if the -u option is supplied.  The vari‐
          able MAPFILE is the default array.  Options, if supplied, have the following meanings:
          -n     Copy at most count lines.  If count is 0, all lines are copied.
          -O     Begin assigning to array at index origin.  The default index is 0.
          -s     Discard the first count lines read.
          -t     Remove a trailing newline from each line read.
          -u     Read lines from file descriptor fd instead of the standard input.
          -C     Evaluate callback each time quantum lines are read.  The -c option specifies quantum.
          -c     Specify the number of lines read between each call to callback.

          If  -C  is specified without -c, the default quantum is 5000.  When callback is evaluated, it is supplied the index of the next array element
          to be assigned as an additional argument.  callback is evaluated after the line is read but before the array element is assigned.

          If not supplied with an explicit origin, mapfile will clear array before assigning to it.

          mapfile returns successfully unless an invalid option or option argument is supplied, array is invalid or unassignable, or if array is not an
          indexed array.



回答2:


It's actually a lot easier than any answers provided thus far. You don't need to call any external binaries such as cat or do any loops.

Simply do:

array=( $(ispell -l < $1) )



回答3:


There is no such thing as an array in shell. (Bash and zsh have array extensions; but if you find yourself thinking that a bash or a zsh extension would be helpful for a script, the correct choice is instead to rewrite in perl or python. /digression)

What you actually want is one of these constructs:

ispell -l < "$1" | while read line; do
    ... take action on "$line" ...
done

or

for word in $(ispell -l < "$1"); do
    ... take action on "$word" ...
done

I would need to know more about what you're trying to do and about the output format of ispell -l (I don't have it installed and don't have time right now to build it) to tell you which of the above is the right option.




回答4:


#!/bin/bash

declare -a ARRAY_VAR=(`cat $1 | ispell -l`)

for var in ${ARRAY_VAR[@]}
do
    place your stuff to loop with ${VAR} here
done



回答5:


you can pipe the result of ispell to awk

ispell -l $file | awk '
{
  print "Do something with $0"
  #Or put to awk array
  array[c++]=$0
}
END{
  print "processing the array here if you want"
  for(i=1;i<=c;i++){
     print i,array[i]
  }
}
'

Awk has better performance for iterating through files than the shell.



来源:https://stackoverflow.com/questions/5542078/can-i-pipe-ispell-output-to-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!