Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure/trouble combining two scripts

Status
Not open for further replies.
Jun 3, 2007
84
0
0
US
Hello everyone I wondering if someone could give me some pointers/help with how to go about combining the following two scripts into one. I have a script that generates lines of text and a second script which will sort a file based on a unique pattern matched by Regex expression.

First script that generates/creates ID and other information.


if ($ARGV[0] eq "new") {
@hex = system("/home/testuser01/new.php $ARGV[0] > $path$r_file");
open(TEMP, "$path$r_file") || die "Count not open resource file";
open(ORIGINAL_CERT, "$path$old_id") || die "Count not open old ID file";
@gen_array1 = <ORIGINAL_GEN>;
open(NEW_GEN, ">/home/testuser01/new-gen.txt") || die "Could not open new generator file";

foreach $line(@certarray1) {
if($line =~ /id:\s?(\d+)/) {
print $line, "\n";
chomp $line;

if($line =~ /id:\s?(\d+)//)
{
$line =~ $line =~ s/.*id:\s?(\d+).*//sgi;
print NEW_GEN $line, "\n";
}


else {
print NEW_GEN $line, "\n";
while (<TEMP>) {
chomp;
next if /^\s*$/;
$id++;
$num++;
@array1 = split;
print NEW_GEN "id:$id" , "data:\"$count\"\; " , "identifier:", "\"|@array1\"\; " , "\n";
}
}
}
}
}


close($ORIGINAL_CERT);
close($NEW_CERT);
close($HAN2);




The second script does the following
Reads in a file line by line, each line is being placed into an array. A sample file contains lines of text as shown below unsort with comments etc....


#Comment
*ss id addr id: 099 bbb ccc aaa
*ss id addr id: 003 aaa bbb ccc
#Comment 2
*333 23 ss id: 002 aaa bbb ccc
*22 233333333 34432 233 44


What I am trying to do is sort the lines by the pattern "/id\:\s?(0-9)" and if a line does not contain the "id: number" then just print the line as it is in the file. so the new output file would contain the following: They are being sorted by the ID: field followed by the number. Final output after sorting looks like below.


#Comment
*333 23 ss id: 002 aaa bbb ccc
*ss id addr id: 003 aaa bbb ccc
#Comment 2
*ss id addr id: 099 bbb ccc aaa
*22 233333333 34432 233 44





Second script that sorts based on a pattern match and prints all other lines without changing them.


unlink("$path$rfile");
open(FILE, "/home/testuser01/") || die "Count not open old Gen_file";
@lines = <FILE>;
@matching_indices = grep $lines[$_] =~ /id\:\s?\d/, 0 .. $#lines;
@matching_lines = @lines[@matching_indices];
@sorted_matching_lines = sort { my ($an) = ( $a =~ /id\:\s?(\d+)/ );
my ($bn) = ( $b =~ /id\:\s?(\d+)/ );
$an <=> $bn
} @matching_lines;
@lines[@matching_indices] = @sorted_matching_lines;
print @lines;


So what I would like to do is add the sorting portion of the second script to the first, that way everything would be under one script. I am at this point stuck since what I have tried does not seem to be working. So any ideas on how to append the sort porting of the code to the first script would really help me out as I think I have been looking at this so long now that I am just confusing myself.

thanks for all help in advance.
 
Everywhere you print to NEW_GEN change that to push to a array, then use that array instead of @lines=<FILE>;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top