for example i have a text file with 5 lines:
one
two
three
four
five
and i want to make a script to make a 2000 lines file containing loops of the file above and it would look like
one
two
three
four
five
one
two
three
four
five
one
two
three
four
five
............repeat until n times is reached
Testing showed this to be about 100 times faster than the next best approach given so far.
#!/bin/bash
IN="${1}"
OUT="${2}"
for i in {1..2000}; do
echo "${IN}"
done | xargs cat > "${OUT}"
The reason this is so much faster is because it doesn't repeatedly open, seek to end, append, and close the output file. It opens the output file once, and streams the data to it in a single large, continuous write. It also invokes cat as few times as possible. It may invoke cat only once, even, depending on the system's maximum command line length and the length of the input file name.
If you need to repeat 2000 times
for i in {1..2000}; do cat "FILE"; done > NEW_FILE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With