Help with printing sorted expression


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with printing sorted expression
# 8  
Old 04-28-2011
Code:
#!/usr/bin/perl
print "Enter minimum coverage: ";
chomp($min_cov=<STDIN>);
print "Enter minimum novel_Allele_Starts: ";
chomp($min_nas=<STDIN>);
open I, "$ARGV[0]";
while (<I>){
  @F=split;
  $F[8]=~/coverage=([^;]+)/;
  $cov=$1;
  $F[8]=~/novelAlleleStarts=([^;]+)/;
  $nas=$1;
  if ($cov>=$min_cov && $nas>=$min_nas){
    $cov{$F[0]}{$F[3]}+=$cov;
    $nas{$F[0]}{$F[3]}+=$nas;
  }
  $n{$F[0]}{$F[3]}++;
  $h{$F[0]}++;
}
END{
  @sorted_cov=sort {$a cmp $b} keys %cov;
  for $i (@sorted_cov){
    for $j (keys %{$cov{$i}}){
      print "$i\t$j\t";
      printf "%.1f\t", ($cov{$i}{$j}/$n{$i}{$j});
      printf "%.1f\n", ($nas{$i}{$j}/$n{$i}{$j});
    }
  }
  @sorted_h=sort {$a cmp $b} keys %h;
  for $i (@sorted_h){
    print "$i\t$h{$i}\n";
    $t+=$h{$i};
  }
}

Note that sort function doesn't need to be executed inside a "for" loop. It was working in your code, but it was sorting that hash the same way multiple times. Also pay attention to make proper indents, as it will help you alot, when your code will grow bigger.

Last edited by bartus11; 04-28-2011 at 03:57 PM..
This User Gave Thanks to bartus11 For This Post:
# 9  
Old 04-29-2011
Thanks for the tip about indenting and your input ... Yeah its very important to have a clean readable code ... by the way I had to change above code for following lines as follows to get the expected outcome, otherwise it was printing the results of %h for the whole file and not just the lines passing the criteria set at STDIN.
Code:
  if ($cov>=$min_cov && $nas>=$min_nas){
    $cov{$F[0]}{$F[3]}+=$cov;
    $nas{$F[0]}{$F[3]}+=$nas;
    $n{$F[0]}{$F[3]}++;
    $h{$F[0]}++;
  }
}

Cheers Bartus ... Hv a nice day ahead Smilie

---------- Post updated at 08:28 AM ---------- Previous update was at 03:49 AM ----------

@Bartus:
Hi teacher, I have a question. So in the above example I'm trying to print at the very end the entire set of lines for which the IF conditions are met.
I tried
Code:
$lines=join ("", $_);

or
Code:
$lines=$_++;

within the if loop and the printing $lines in the end but didnt get it ... it just printed one of the lines from the input file.
Can you explain what mistake I am making and how I can correct it?
Cheers Smilie
# 10  
Old 04-29-2011
Try:
Code:
if ($cov>=$min_cov && $nas>=$min_nas){
    $cov{$F[0]}{$F[3]}+=$cov;
    $nas{$F[0]}{$F[3]}+=$nas;
    $n{$F[0]}{$F[3]}++;
    $h{$F[0]}++;
    $lines.=$_;
  }

I didn't test it though.
# 11  
Old 04-29-2011
I tested ... it works .... JUST ONE DOT was missing ...cant beleive!!!
Its a concatenation operator of lines in this case right ??
Cheers Master Smilie
Hv a nice weekend Smilie
# 12  
Old 04-29-2011
Quote:
Originally Posted by pawannoel
Its a concatenation operator of lines in this case right ??
Yes Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. UNIX for Dummies Questions & Answers

Help with printing sorted array of numbers

Dear All, I am trying to sort an array of numbers to retrieve the mimimum and maximum values of numbers in that array, by printing the first and last elements of the sorted array. My code is @sorted_numbers = sort (@numbers); print "@sorted_numbers\n"; print "$sorted_numbers,... (0 Replies)
Discussion started by: pawannoel
0 Replies

4. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

5. Shell Programming and Scripting

Printing several lines of a file after a specific expression

Hello, I want to print a number of lines of a file after a specific expression of a line. I have this sed command but it prints only 1 line after the expression. How could I adapt it to print for instance 10 lines after or 15 lines after ? sed -n '/regexp/{n;p;}' Thx & Regs, Rany. (5 Replies)
Discussion started by: rany1
5 Replies

6. Shell Programming and Scripting

sed not printing lines before a regular expression.

Hey, I found a way to print the lines which is just before a regular expression, not including the expression. sed -n '/regexp/{n;p;}' myfile Now I'm looking for a way to print all lines, exept the regular expression and also the line before the same regular expression. Use code tags. (1 Reply)
Discussion started by: Livio
1 Replies

7. UNIX for Advanced & Expert Users

Sorted file

Hi Is there any unix shell command or utility to know if the file is sorted or not? Thanks (3 Replies)
Discussion started by: ksailesh
3 Replies

8. Shell Programming and Scripting

SED printing just parts of lines that match an expression

Hi - I am guessing this is fairly simple for someone .. but I can not quite figure it out. I need a sed command to print just parts of lines from a file. e.g. filea.txt 4710451 : Success : MODIFY : cn=user1,dc=org,dc=uk Message log started Message log ended 4710452 : Success : MODIFY :... (7 Replies)
Discussion started by: sniper57
7 Replies

9. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies

10. Shell Programming and Scripting

sorted processes

Hi, I am trying to make a script that creates a list of all active (alive) processes sorted by size and then print this list on screen. Could anyone help me? Thaks a lot (7 Replies)
Discussion started by: pro
7 Replies
Login or Register to Ask a Question