简体   繁体   English

使用bash将列打印到文本

[英]Printing a column to a text using bash

I have a shell script to print a column to text file : 我有一个Shell脚本将一列打印到文本文件:

#!/bin/bash  
for i in `seq 1 1 174492`;  
do  
for j in `seq 0 100 14000`;  
do  
echo "$j" >> "depth"  
done  
done  

But the program is taking too long. 但是该程序花费的时间太长。 Is there a better way to do this? 有一个更好的方法吗?

Use built-in brace expansion rather than calling seq , and redirect the whole outer loop rather than opening and closing the file once per iteration of the inner loop: 使用内置的括号扩展而不是调用seq ,并重定向整个外部循环,而不是每次内部循环迭代都打开和关闭文件一次:

for i in {1..174492}  
do  
    for j in {0..14000..100}  
    do  
        echo "$j"  
    done  
done >> "depth"

Now your overhead is the loops themselves, so if that's still not fast enough for you, then use a faster language: 现在您的开销就是循环本身,因此,如果仍然不够快,请使用更快的语言:

awk 'BEGIN { 
    for (i = 1; i <= 174492; ++i) 
        for (j = 0; j <= 14000; j += 100) print j
}' >> depth

I tested this on my system and it took 8 seconds, whereas the shell loop took over 2 minutes. 我在系统上进行了测试,耗时8秒,而Shell循环耗时2分钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM