views:

319

answers:

9

I would like to create 1000+ text files with some text to test a script, how to create this much if text files at a go using shell script or Perl. Please could anyone help me.

+7  A: 

Can you create one text file?

Can you devise a scheme to generate file names, eg. file0001, file002?

Can you write a for loop to do those two tasks.

At what point do you get stuck?

djna
I can create one text file, I'm stuck at the loop and file name sequence.
Kaartz
@djna - this should be a comment, not an answer
David Dorward
I get stuck when I try and work out what the 3rd file should be named. file03? What happens when I get to the 5th file? :)
Robert Grant
I disgree, it's an answer given by a sequence of questions. I'm explaining how to break up the problem and think about it. Difference between giving a fish and teaching how to fish. Also I'm trying to show that asking a better question would give a better answer.
djna
+1  A: 

I don't know in shell or perl but in python would be:

#!/usr/bin/python

for i in xrange(1000):
    with open('file%0.3d' %i,'w') as fd:
        fd.write('some text')

I think is pretty straightforward what it does.

fabrizioM
I tried your script, but I'm getting an error:./crea.py:4: Warning: 'with' will become a reserved keyword in Python 2.6 File "./crea.py", line 4 with open('file%0.3d' %i, 'w') as fd: ^SyntaxError: invalid syntax
Kaartz
try from __future__ import with_statement
ghostdog74
That would be `from __future__ import with_statement` (the system ate the underscores in **ghostdog74's** comment).
Dennis Williamson
+7  A: 
#!/bin/bash
seq 1 1000 | split -l 1 -a 3 -d - file

Above will create 1000 files with each file having a number from 1 to 1000. The files will be named file000 ... file999

Damodharan R
+6  A: 
for i in {0001..1000}
do
  echo "some text" > "file_${i}.txt"
done

or if you want to use Python <2.6

for x in range(1000):
    open("file%03d.txt" % x,"w").write("some text")
ghostdog74
Your filenames in the Bash version will be 10 through 1000999. I think you mean `echo "some text" > file$file`
Dennis Williamson
yes, thks for spotting.
ghostdog74
The first option requires Bash 4 to make use of the padding. In earlier versions, you can specify leading zeros, but they won't appear in the result.
Dennis Williamson
+2  A: 
#!/bin/bash

for suf in $(seq -w 1000)
do
        cat << EOF > myfile.$suf
        this is my text file
        there are many like it
        but this one is mine.
EOF
done
BobS
+1  A: 

You can use only Bash with no externals and still be able to pad the numbers so the filenames sort properly (if needed):

read -r -d '' text << 'EOF'
Some text for
my files
EOF

for i in {1..1000}
do
    printf -v filename "file%04d" "$i"
    echo "$text" > "$filename"
done

Bash 4 can do it like this:

for filename in file{0001..1000}; do echo $text > $filename; done

Both versions produce filenames like "file0001" and "file1000".

Dennis Williamson
+3  A: 

In Perl:

use strict;
use warnings;

for my $i (1..1000) {
   open(my $out,">",sprintf("file%04d",$i));
   print $out "some text\n";
   close $out;
}

Why the first 2 lines? Because they are good practice so I use them even in 1-shot programs like these.

Regards, Offer

Offer Kaye
While you are advocating good practices (and rightfully so), you may as well check for success on open ... or die "can not open file: $!";
toolic
... or you could just `use autodie;`
Brad Gilbert
+2  A: 

For variety:

#!/usr/bin/perl

use strict; use warnings;
use File::Slurp;

write_file $_, "$_\n" for map sprintf('file%04d.txt', $_), 1 .. 1000;
Sinan Ünür
A: 

Here is a short command-line Perl program.

perl -E'say $_ $_ for grep {open $_, ">f$_"} 1..1000'
Brad Gilbert