Duplicate Rows in Text File

I need to duplicate rows in text file with a specific number of times. For example my data file is:

jplg3350.18i
jplg3360.18i
jplg3370.18i

I need to duplicate the lines three times as follows;

jplg3350.18i
jplg3360.18i
jplg3370.18i
jplg3350.18i
jplg3360.18i
jplg3370.18i
jplg3350.18i
jplg3360.18i
jplg3370.18i
1

1 Answer

For 3 times you can just run:

cat file file file > new_file

And here is a trick if you're lazy like me and you don't want to re-type the file name N times. Type cat then the filename, Press Ctrl+W, then Ctrl+YSpace N times, finally type > new_file.


However it's a better idea to use a simple "loop" in combination with cat command.

  • 3 times example:

    for i in {1..3}; do cat file >> new_file; done
    

Or as you asked in comments:

limit=3
for ((i=0; i<limit; i++)); do cat file >> new_file; done

Change '3' to any number you want.

Result:

jplg3350.18i
jplg3360.18i
jplg3370.18i
jplg3350.18i
jplg3360.18i
jplg3370.18i
jplg3350.18i
jplg3360.18i
jplg3370.18i
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest