我可以通过管道将 ispell 输出传送到数组吗?

发布于 2024-10-30 14:07:02 字数 154 浏览 3 评论 0原文

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

到目前为止,

cat $1 | ispell -l

我已经尝试将它们逐行读取到数组中,但这对我没有

任何建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

往日 2024-11-06 14:07:02

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

只需执行以下操作:

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

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) )
内心荒芜 2024-11-06 14:07:02

最近的 bash 附带了 mapfilereadarray:(

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

此示例有效返回 ispell -l <​​ "$1"|wc -l< /em>)

请注意错误,例如 ls | readarray,它不会工作,因为由于管道的原因,readarray 将位于子 shell 中。仅使用输入重定向。


   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.

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.
污味仙女 2024-11-06 14:07:02

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

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.

戏蝶舞 2024-11-06 14:07:02
#!/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
#!/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
时光暖心i 2024-11-06 14:07:02

您可以将 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 具有更好的性能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文