Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text file multiply bash linux

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
like image 509
newmusicblog Avatar asked Feb 09 '26 16:02

newmusicblog


2 Answers

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.

like image 140
Dan Moulding Avatar answered Feb 12 '26 15:02

Dan Moulding


If you need to repeat 2000 times

for i in {1..2000}; do cat "FILE"; done > NEW_FILE
like image 29
ajreal Avatar answered Feb 12 '26 14:02

ajreal