如何使用 Bash 快速将大量字符串输入到文件中
我需要创建一堆字母的排列,后跟数字并将其放入文件中。我有一堆嵌套的 for 循环,如下所示,然后将其回显到文件中,但这非常慢,可能需要一个小时或更长时间(我懒得计算多长时间)。我怎样才能更快地做到这一点,我认为缓慢的部分是我多次打开和关闭文件。
for a in {a..z}
do
for b in {a..z}
do
for c in {a..z}
do
for i in {1..100}
do
echo "$a$b$c$i" >> permutations
done
done
done
done
是的,我知道 Bash 不是最好的语言,但它是我必须使用的。
有什么想法吗?
I need to create a bunch of permutations of letters, followed by numbers and place it into a file. I have a bunch of nested for loops, as shown below, and then am echoing it into the file, but this is extremely slow, as in probably an hour or more (I am too lazy to figure how long). How can I do this faster, I think the slow part is that I am opening and closing the file way to many times.
for a in {a..z}
do
for b in {a..z}
do
for c in {a..z}
do
for i in {1..100}
do
echo "$a$b$c$i" >> permutations
done
done
done
done
Yes I know Bash is not the best language for this, but it is what I have to use.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Bash 大括号扩展 可以做到这一点:
如果你想要每个在新行上进行排列,您可以添加替换:
Bash brace expansion can do this:
If you want each permutation on a new line you can add a substitution: