【问题标题】:Can I pipe ispell output to an array?我可以将 ispell 输出通过管道传输到数组吗?
【发布时间】:2011-04-04 17:27:03
【问题描述】:

我需要以某种方式将 ispell -l 的输出放入一个数组中,以便循环遍历它们。

到目前为止,我得到了这个

cat $1 | ispell -l

我试图将它们逐行读取到一个数组中,但这对我不起作用

有什么建议吗?

【问题讨论】:

  • 正则表达式呢?只需将其输出到文件 `> 文件',然后使用正则表达式或使用简单的脚本逐行循环,没什么大不了的。
  • 有没有办法设置数组=文件或者我需要逐行读取

标签: arrays shell unix pipe


【解决方案1】:

最近的 bash 带有 mapfilereadarray

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

(这个例子有效地返回ispell -l &lt; "$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.

【讨论】:

    【解决方案2】:

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

    简单地做:

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

    【讨论】:

      【解决方案3】:

      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 的输出格式(我没有安装它,现在没有时间构建它)告诉你哪个以上是正确的选择。

      【讨论】:

        【解决方案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】:

          您可以将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 在遍历文件方面比 shell 有更好的性能。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-17
            • 1970-01-01
            • 1970-01-01
            • 2014-08-17
            相关资源
            最近更新 更多