-1

Here I want to write some shell code and my question is, I want to copy one data file multiple times into another new file. For example: File1 contains 3000 lines of data. Now I want this data multiple times in another single file (File1 * 3 > File2). Here I am copying File1's data 3 times and saving it to File2. Now File2 contains 12000 lines of data.

Thanks.

5

If you prefer loops:

for i in {1..3}; do cat file1 >> file2; done

[edit]

how to give dynamically n value in for loop on shell script

put this into myCopyScript.sh

#!/bin/bash
for i in {1.. $3 }; do cat $1 >> $2; done

make the it executable

chmod u+x myCopyScript.sh

then call it like this:

myCopyScript.sh file1 file2 4711
  • 1
    Though of course this command is slightly different to Konstantin's answer, in that if file2 already exists its existing contents will be retained and file1 appended three times after its existing contents. – Muzer Nov 11 '16 at 9:11
  • @Muzer sure, but I guess this is acceptable... ;o) – Timothy Truckle Nov 11 '16 at 9:13
  • 1
    Indeed, I was just mentioning it! – Muzer Nov 11 '16 at 9:20
  • @Muzer can u help me on this question: how to give dynamically n value in for loop on shell script... for((i=1;i<n;i++)).. I will give n value as a input!!Hope u got my point – Akhil Kumar Patnaik Nov 11 '16 at 12:06
  • Where's the input coming from? You can just do something like i<"$var" where $var is your variable (eg "$1" for the first command-line input). And as Timothy says, you should mark an answer as accepted by clicking the tick. – Muzer Nov 11 '16 at 12:14
5

Simple solution

cat File1 File1 File1 > File2
  • If i want 70 times then ?? i will put cat File1 File2..File 70. Hope u understand what am saying..anyways thanks fr ur comment. And cat is the basic command buddy. – Akhil Kumar Patnaik Nov 11 '16 at 9:57
  • It's just solution for small i. If you want more, use @TimothyTruckle answer. – Konstantin Morenko Nov 11 '16 at 11:06
1
    #!/bin/sh
echo "Enter data file name or exact path:"
read f
echo "Enter new file name:"
read nf
echo "Enter how many times you want to copy:"
read n
for (( i=1 ; i<=$n; i++)); do
cat $f >> $nf ;
done

Your Answer

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

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