Creating a sequence of numbers in a line for 1000 files

Hi, I try to explain my problem , I have a file like this:

aasdsaffsc23
scdsfsddvf46567
mionome0001.pdb
asdsdvcxvds
dsfdvcvc2324w

What I need to do is to create 1000 files in which myname line listing a sequence of numbers from 0001 to 1000. So I want to have :
nomefile0001.txt that must have the line mionome0001.pdb
nomefile0002.txt that must have the line mionome0002.pdb
and so on up to nomefile1000.txt with the line mionome1000.pdb

Can you give me some advice ? Maybe I could use sed ... but how ??? Thanks a lot to everyone

Not too clear. Do you want 1000 output files containing

aasdsaffsc23
scdsfsddvf46567
mionome0001.pdb  ( - mionome1000.pdb)
asdsdvcxvds
dsfdvcvc2324w

, i.e have the same contents except for the third line that will be numbered ascendingly (guessing that "myname" equals "mionome")?

is correct, the file must have the same content except for that line

You mentioned mionome0001.pdb . What happens with the other lines in the file?

i want to change only the line mionome0001.pdb
the other lines are unchanged in all the 1000 files

Try

awk ' 
        {P[NR] = $1
         if (/mionome/) IX = NR
        }
END     {for (i=1; i<=CNT; i++) {T = sprintf ("%04d.", i)
                                 FN = "nomefile" T "txt" 
                                 sub (/[0-9]+\./, T, P[IX])
                                 for (j=1; j<=NR; j++) print P[j] > FN
                                 close (FN) 
                                }
        }
' CNT=1000 file

my real file is this:

outlev 1
parameter_file AD4.1_bound.dat
intelec
seed pid time
ligand_types A C OA N NA S SA HD F Cl Br
fld 3dct_apo_box50.maps.fld
map 3dct_apo_box50.A.map
map 3dct_apo_box50.C.map
map 3dct_apo_box50.OA.map
map 3dct_apo_box50.N.map
map 3dct_apo_box50.NA.map
map 3dct_apo_box50.S.map
map 3dct_apo_box50.SA.map
map 3dct_apo_box50.HD.map
map 3dct_apo_box50.F.map
map 3dct_apo_box50.Cl.map
map 3dct_apo_box50.Br.map
elecmap 3dct_apo_box50.e.map
desolvmap 3dct_apo_box50.d.map
move 4e4k_lig.pdbqt
about 20.088 64.795 18.258
tran0 random
quat0 random
dihe0 random
tstep 2.0
qstep 50.0
dstep 50.0
rmstol 2.0
extnrg 1000.0
e0max 0.0 10000
ga_pop_size 300
ga_num_evals 5000000
ga_num_generations 27000
ga_elitism 1
ga_mutation_rate 0.02
ga_crossover_rate 0.8
ga_window_size 10
ga_cauchy_alpha 0.0
ga_cauchy_beta 1.0
set_ga
sw_max_its 300
sw_max_succ 4
sw_max_fail 4
sw_rho 1.0
sw_lb_rho 0.01
ls_search_freq 0.06
set_sw1
ga_run 1000
analysis

i want to change only 4e4k_lig.pdbqt in 4e4k_lig0001.pdbqt ecc ecc for all 1000 files.

with your command, the result is this:

outlev
parameter_file
intelec
seed
ligand_types
fld
map
map
map
map
map
map
map
map
map
map
map
elecmap
desolvmap
move0051.4k_lig.pdbqt
about
tran0
quat0
dihe0
tstep
qstep
dstep
rmstol
extnrg
e0max
ga_pop_size
ga_num_evals
ga_num_generations
ga_elitism
ga_mutation_rate
ga_crossover_rate
ga_window_size
ga_cauchy_alpha
ga_cauchy_beta
set_ga
sw_max_its
sw_max_succ
sw_max_fail
sw_rho
sw_lb_rho
ls_search_freq
set_sw1
ga_run
analysis

You said:

But then you changed move 4e4k_lig.pdbqt for move0051.4k_lig.pdbqt , in the I/O examples.

If you keep changing your requirements and give us sample input that is not at all representative of your actual input, you are very likely to get suggestions that won't do what you want. If what you really meant in post #4 was that you want to change all occurrences of 4e4k_lig.pdbqt in your input file to 4e4k_lig0001.pdbqt through 4e4k_lig1000.pdbqt in your output files, you could try something like:

#!/bin/ksh
IAm=${0##*/}
Usage='Usage: %s [count=file_count] [filename_format="format"] [pat="ERE"] \\
		[rep_format="format"] file
where the optional operands specify:
	count=file_count
		The number of output files to create from the file operand.
		Default 1000.
	filename_format="format"
		The sprintf() format string used to create output file
		pathnames.  Default value is "nomefile%04d.txt".
	pat="ERE"
		An extended regular expression defining the text to be matched
		in file.  Default value is "4e4k_lig[0-9]{0,4}.pdbqt".
	rep_format="format"
		The sprintf() format string used to create the replacement text
		for each string matching pat in file.  Default value is
		"4e4k_lig%04d.pdbqt".
Note that the file operand must be presented after any optional operands.
'
if [ $# -lt 1 ]
then	printf "$Usage" "$IAm" >&2
	exit 1
fi
awk '
BEGIN {	count = 1000
	filename_format = "nomefile%04d.txt"
	pat = "4e4k_lig[0-9]{0,4}.pdbqt"
	rep_format = "4e4k_lig%04d.pdbqt"
}
{	l[NR] = $0
}
END {	for(i = 1; i <= count; i++) {
		fn = sprintf(filename_format, i)
		rep = sprintf(rep_format, i)
		for(j = 1; j <= NR; j++) {
			line = l[j]
			gsub(pat, rep, line)
			print line > fn
		}
		close(fn)
	}
}' "$@"

Although written and tested using a Korn shell, this script should work with any shell that uses Bourne shell syntax. If you want to use this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

When invoked with just a filename as an operand, it will process that file making the changes requested in post #4 in this thread. If invoked with:

./script_name pat="mionome0001.pdb" rep_format="mionome%04d.pdb" file1

it will also produce the output files you requested in post #1 in this thread.

Example to run it:

Save as blastaway.pl
Run as perl blastaway.pl danyz84.txt

Pay attention to the comments in blue and modify variables to your need.

#!/usr/bin/perl
use strict;
use warnings;

my @template_lines;
my ($target, $match);
my $search = "4e4k_lig"; # Change me to the pattern you want to match.

while (<>) {
    push @template_lines, $_;
    /$search/ and $target = ($. -1) and $match = $_;
}
die unless $target;

my $amount_of_files = 10; # Change me to 1000.
my $file_base = "nomefile"; # Change me to a file base name.

for my $x (1 .. $amount_of_files){
    my $serial = sprintf "%04d", $x;
    my $mod = $match;
    $mod =~ s/$search/$search$serial/;
    $template_lines[$target] = $mod;
    createfile($serial);
}

sub createfile {
    my $n = shift;
    open my $fh, '>', "$file_base$n.txt" or die "Could not write file: $!";
    print $fh @template_lines;
    close $fh;
}

Great!!! Thanks