我可以通过管道将 ispell 输出传送到数组吗?
我需要以某种方式将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它实际上比迄今为止提供的任何答案都要容易得多。您不需要调用任何外部二进制文件(例如
cat
)或执行任何循环。只需执行以下操作:
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:
最近的 bash 附带了
mapfile
或readarray
:(此示例有效返回
ispell -l < "$1"|wc -l
< /em>)请注意错误,例如
ls | readarray
,它不会工作,因为由于管道的原因,readarray 将位于子 shell 中。仅使用输入重定向。Recent bash comes with
mapfile
orreadarray
:(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.shell 中没有数组这样的东西。 (Bash 和 zsh 具有数组扩展;但是如果您发现自己认为 bash 或 zsh 扩展对脚本有帮助,那么正确的选择是用 perl 或 python 重写。/题外话)
您真正想要的是以下之一这些构造:
或者
我需要更多地了解您想要做什么以及
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:
or
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.您可以将
ispell
的结果通过管道传递给awk
awk
在迭代文件方面比 shell 具有更好的性能。you can pipe the result of
ispell
toawk
Awk
has better performance for iterating through files than the shell.