3

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
10

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
  • 2
    You couldn't even let the little guys take this one! Happy New Year Rav! – George Udosen Jan 7 at 10:53
  • :)) Happy to you too George ;) – Ravexina Jan 7 at 10:54
  • @George, thank you very much. – deepblue_86 Jan 7 at 11:07
  • 1
    Another lazy trick is to use brace expansion with an empty string: cat file{,,} > new_file – wjandrea Jan 7 at 14:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.