2011-04-04 70 views
0

我需要以某种方式获得的ispell -l的输出到一个数组,这样我就可以通过他们循环..我可以将ispell输出传递给数组吗?

到目前为止,香港专业教育学院得到这个

cat $1 | ispell -l 

我曾试图通过阅读他们的线线成阵列,但没有为我工作

有什么建议吗?

+0

什么正则表达式?只需将它输出到文件'> file',然后使用正则表达式或使用一个简单的脚本逐行循环,而不是使用biggy。 – tjameson 2011-04-04 17:38:37

+0

有没有办法设置一个数组=文件还是我需要逐行阅读 – bluetickk 2011-04-04 17:50:54

回答

1

shell中没有这样的数组。 (Bash和zsh有数组扩展;但如果你发现自己认为bash或zsh扩展会对脚本有帮助,那么正确的选择是用perl或python。/题外重写)

你究竟要的就是这些结构之一:

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

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

我需要更多地了解你正在试图做什么,关于ispell -l输出格式(我没有它安装,现在没有时间来构建它)告诉你以上哪一项是正确的选择。

0
#!/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 
2

最近的bash自带mapfilereadarray

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

该示例中有效地返回ispell -l < "$1"|wc -l

提防错误例如做的ls | readarray,它不会工作,因为readarray将因为管道而处于子外壳中。仅使用输入重定向。


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. 
0

你可以管的ispell的结果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有通过比外壳文件迭代更好的性能。

2

这实际上比迄今为止提供的答案容易得多。您无需调用任何外部二进制文件,如cat或执行任何循环。

简单地做:

array=($(ispell -l < $1)) 
相关问题