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_GEN, "$path$old_id") || die "Count not open old ID file"; @gen_array1 = ; open(NEW_GEN, ">/home/testuser01/new-gen.txt") || die "Could not open new generator file"; foreach $line(@gen_array1) { 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 () { chomp; next if /^\s*$/; $id++; $num++; @array1 = split; print NEW_GEN "id:$id" , "data:\"$count\"\; " , "identifier:", "\"|@array1\"\; " , "\n"; } } } } } close($ORIGINAL_GEN); close($NEW_GEN); close($TEMP); [/code 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 #### #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 #### unlink("$path$rfile"); open(FILE, "/home/testuser01/") || die "Count not open old Gen_file"; @lines = ; @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;