简体   繁体   中英

Printing a column to a text using bash

I have a shell script to print a column to text file :

#!/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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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