Creating a sequence of numbers in a line for 1000 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a sequence of numbers in a line for 1000 files
# 8  
Old 05-14-2016
You said:
Quote:
i want to change only 4e4k_lig.pdbqt in 4e4k_lig0001.pdbqt ecc ecc for all 1000 files.
But then you changed move 4e4k_lig.pdbqt for move0051.4k_lig.pdbqt, in the I/O examples.

Last edited by Aia; 05-14-2016 at 04:57 PM.. Reason: OP changed requirements
# 9  
Old 05-14-2016
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:
Code:
#!/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:
Code:
./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.
# 10  
Old 05-15-2016
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.

Code:
#!/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;
}

# 11  
Old 05-16-2016
Great!!! Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating sequence number as per records

Hi, I have a file source as below. OL|10031|Day|Black|Midi|Good|Value|P01|P07 OL|10031|Day|Black|Short|Good|Value|P01|P07 I need to create a file form the above data as below logic 1. take the first line 2. create a file say inclusion1 as below from the first line OL,10031,1,Day... (8 Replies)
Discussion started by: bhaski2012
8 Replies

2. Shell Programming and Scripting

Script to generate sequence of numbers

I need awk script to generate part number sequencing based on data in multiple columns like below Input File --------- Col A|Col B|Col C| 1|a|x| 2|b|y| |c|z| | |m| | |n| And out put should be like 1ax 1ay 1az 1am 1an 1bx 1by (6 Replies)
Discussion started by: aramacha
6 Replies

3. UNIX for Dummies Questions & Answers

Replace lines of two files by the corresponding line numbers.

I want to replace lines. The files 1 are (separated by \t) Gm01 phytozome9_0 three_prime_UTR 70641 70759 . - . ID=PAC:26323927.three_prime_UTR.1;Parent=PAC:26323927;pacid=26323927 Gm01 phytozome9_0 three_prime_UTR 90230 90692 . - . ... (1 Reply)
Discussion started by: grace_shen
1 Replies

4. Shell Programming and Scripting

Adding a line to 1000's of files right after x amt of characters.

I am trying to add a single line of text to every file in a particular folder. There are thousands of files in the folder. Each file contains this same start of the first line: {d "%w- %d %m-, %Y - %T"} <some message here> with the rest of the text following the second curly bracket... (10 Replies)
Discussion started by: dlundwall
10 Replies

5. Shell Programming and Scripting

Perl verify if numbers in a column of a file are in sequence

I am just a newbie to perl scripting. I need help with listing of hexadecimal numbers in a column as follows. INPUT FIle: 08AF ship steel 08B0 ship steel 08B1 ship steel 08B2 flight docs 08B3 flight docs 08B4 flight docs 08B5 flight docs 08B6 flight decl ... (3 Replies)
Discussion started by: dynamax
3 Replies

6. Shell Programming and Scripting

Need to find the gap in the sequence of numbers

Hi Guys, I have a file with numbers in sequence. The sequence have been broken somewhere.. I need to find out at which number the sequence has been broken... For an example, consider this sequence, it needs to give me output as 4 (as 5 is missing) and 6(as 7 is missing) Thanks for... (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Shell Programming and Scripting

Creating 1000 files using a script

I would like to write a script that would create 1000 files like clm0001.txt to clm1000.txt. All the files would contain the same contents. How do I achieve this using a script?Please help. (2 Replies)
Discussion started by: ggayathri
2 Replies

8. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

9. UNIX for Dummies Questions & Answers

creating sequence numbers in unix

Hi, Is there a way to create sequence numbers in unix i have a set of batches(which contain records) and i want to assign a number to every batch. how can i do that? (1 Reply)
Discussion started by: dnat
1 Replies

10. UNIX for Dummies Questions & Answers

how can i isolate the random sequence of numbers using awk?

as you can see there is a delimiter after c8 "::". Awk sees the rest as fields because it doesn't recognize spaces and tabs as delimiters. So i am basically looking to isolate 20030003ba13f6cc. Can anyone help? c8::20030003ba13f6cc disk connected configured unknown (2 Replies)
Discussion started by: rcon1
2 Replies
Login or Register to Ask a Question