I tried with these files:
for ((i=1;i<70000;++i)); do printf -v fn 'file%.5d.sms.txt' $i; echo -e "HAHA\nLOL\nBye" > "$fn"; done
I tried your solution, which took about 4 minutes (real) to process. The problem with your solution is that you open sed 70,000 times! And the turn is pretty slow.
#!/bin/bash filename="sms.txt" # Create file "$filename" or empty it if it already existed > "$filename" # Start editing with ed, the standard text editor ed -s "$filename" < <( # Go into insert mode: echo i # Loop through files for fn in *.sms.txt; do # Loop through lines of file "$fn" while read l; do # Insert line "$l" with "$fn" appended to echo "$l$fn" done < "$fn" done # Tell ed to quit insert mode (.), to save (w) and quit (q) echo -e ".\nwq" )
This solution took approx. 6 seconds
Donβt forget that ed is a standard text editor, and donβt forget about it! If you liked ed , you will also like ex !
Hooray!
source share