0

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 Answers 5

8

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
6
  • 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, 2016 at 9:11
  • @Muzer sure, but I guess this is acceptable... ;o) Nov 11, 2016 at 9:13
  • 1
    Indeed, I was just mentioning it!
    – Muzer
    Nov 11, 2016 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 Nov 11, 2016 at 12:06
  • 1
    @Muzer i want to give the input from console.. For example: echo "enter n:" read n for((i=1;i<n;i++)); do cat x >> y ; done Nov 11, 2016 at 13:02
7

Simple solution

cat File1 File1 File1 > File2
1
  • It's just solution for small i. If you want more, use @TimothyTruckle answer. Nov 11, 2016 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
1

I came across the very same task and the answer by @timothy-truckle has helped a lot.

In order to extend it a bit, I put here my script's code:

if [ $# -ne 3 ]
then
    echo "bash cat_files.sh vocab203nb/vsekz.lm 10 vocab203nbx/vsekz.lm"
    exit 0
fi
fname=$1
times=$2
new_fname=$3
echo "file: $fname"
echo "will be concatenated $times times"
echo "and saved as $new_fname"



for i in $(seq 1 $times); do
    cat $fname >> $new_fname;
done

You can then run the script in the terminal providing the necessary arguments:

bash cat_files.sh src/tmp.lm 4 dst/tmp.lm

where:

src/tmp.lm is a file that should be copied multiple times

4 - is an integer number declaring how many times a file should be concatenated

dst/tmp.lm is a file that should store the result

Hope this helps.

0

In tcsh:

repeat 3 cat file1 > file2

zsh also has a repeat command (a keyword in the language there), but there that needs to be:

{repeat 3 cat file1} > file2

As without the braces, the redirection is taken as part of the command that is repeated.

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.