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
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:
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;
}