Can I pipe ispell output to an array?

2019-06-14 08:31发布

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?

5条回答
该账号已被封号
2楼-- · 2019-06-14 09:11
#!/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
查看更多
戒情不戒烟
3楼-- · 2019-06-14 09:13

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) )
查看更多
beautiful°
4楼-- · 2019-06-14 09:21

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.

查看更多
该账号已被封号
5楼-- · 2019-06-14 09:23

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.
查看更多
成全新的幸福
6楼-- · 2019-06-14 09:26

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.

查看更多
登录 后发表回答