Perl: selective printing of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: selective printing of lines
# 1  
Old 09-12-2011
Perl: selective printing of lines

Hi,

I have a file with lines like this.
Code:
2 7 18 ggcgt anna
2 7 18 hhchc sam
3 7 18 hhdjcc ross
4 7 18 hhcjd  jenny
0 8  21 jjdhs  sam
3 8  21 kkok  bush
2 9  24 kosss  Brenham

If the values of the second column are equal, print only those lines with the least first column value. So in this example, the value 7 is same is in the first 4 lines; and the least first column value among those lines is 2. so I want only the first 2 lines.

desired output is,
Code:
2 7 18 ggcgt anna
2 7 18 hhchc sam
0 8  21 jjdhs  sam
2 9  24 kosss  Brenham

I tried this code but getting all kinds of errors.
Code:
#! usr/bin/perl
use warnings;
@b = ();

while (<>) {

 s/^\s+//g;
 chomp ($count = s/>/">"/g);
chomp($str = "$count $_");
 
  @a = split /\s+/, $str;

 push (@b, @a);
 chomp (@b);
 $i = 0;
 $j = 1;
 
 for ($i < 1000) {

  if ($b[$i] = $b[$i+=18]) {

                if ($b[$j] < $b[$j+=18] ) {


           print $str;}}}}

I want the code only in perl. thanks in advance. Smilie
# 2  
Old 09-12-2011
Code:
$
$
$ cat data.txt
2 7 18 ggcgt  anna
2 9 24 kosss  Brenham
4 7 18 hhcjd  jenny
3 8 21 kkok   bush
2 7 18 hhchc  sam
0 8 21 jjdhs  sam
3 7 18 hhdjcc ross
$
$
$ perl -lne 'push @x, $_;
             END {
               @y = sort { (split " ", $a, 2)[1] <=> (split " ", $b, 2)[1]
                           or
                           (split " ", $a, 2)[0] <=> (split " ", $b, 2)[0]
                         } @x;
               foreach $i (@y) {
                 @k = split / /, $i;
                 print $i if ! defined $pk1 or ($k[1] != $pk1) or ($k[1] == $pk1 and $k[0] == $pk0);
                 $pk0 = $k[0]; $pk1 = $k[1]
               }
             }
            ' data.txt
2 7 18 ggcgt  anna
2 7 18 hhchc  sam
0 8 21 jjdhs  sam
2 9 24 kosss  Brenham
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 09-13-2011
Code:
sort -k2,2 -k1,1 INPUTFILE | perl -lane '                                 
  $F[1] != $prev1                   and $t = 1, print;
  $t && $F[0] == $prev0             and print;
  $F[0] != $prev0 && defined $prev0 and $t = 0 ;
  $prev0 = $F[0]; $prev1 = $F[1]
'

# 4  
Old 09-13-2011
@durden_tyler and @yazu

Thanks a bunch for your replies. I was trying run the script from perl file rather than from the command line and I am getting "uninitialized values $i" in the first code and same error for multiple variables in the second code. Can you please provide the same code in the script mode.

I am beginner of perl and I have been banging my head since today morning to write this script. If possible, can you guys explain the codes briefly ? I really appreciate your help. Thanks again.
# 5  
Old 09-13-2011
I can suggest, that you either:
1. want a work to be done, or
2. want to learn perl, or
3. want to do your homework.
It's not the first case, because you want specifically a perl solution. I can't say it is the second because you don't know about 'my' construction - and you can't learn anything without knowing elementary things. So you don't want to learn perl and it can be suggested that you just want to get a solution for your exercise. Ok, maybe someone will want to do it for you but it's just not me.
# 6  
Old 09-13-2011
Quote:
Originally Posted by yazu
I can suggest, that you either:
1. want a work to be done, or
2. want to learn perl, or
3. want to do your homework.
It's not the first case, because you want specifically a perl solution. I can't say it is the second because you don't know about 'my' construction - and you can't learn anything without knowing elementary things. So you don't want to learn perl and it can be suggested that you just want to get a solution for your exercise. Ok, maybe someone will want to do it for you but it's just not me.
Its actually both 1 and 2. I am trying to finish the work by using/learning perl. I know about "my" construction. But I thought of it as an option as mentioned in the 'basic' perl book. While I appreciate your first reply in this thread, based on your replies in past threads and the second reply in this thread, I reckon you are very judgmental and your comments are presumptuous. Its not that I have not tried; I have provided the code. You either suggest something specific to the code or stay off. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

awk Selective printing of fields

Hi Gurus, I have following input file. I tried multiple awk combinations to print selected columns without success. HEX ID Name ver FLRGT Start Time Total Shared End Date ----- -------- --- ------ ------------------------ -------------- -------... (4 Replies)
Discussion started by: shunya
4 Replies

3. Shell Programming and Scripting

Selective printing based on matched record

Hi, I have file with thousands of lines somewhat similar to the below 6 lines 06MXXXXXXXXXXXXXXXX 0328 003529 J27300022 MICROSOFT *MSN 06<1 000000001344392 JPN151-85830 MSBILL.INFO F 06<A17087454000328651551 MSBILL.INFO ... (16 Replies)
Discussion started by: dsid
16 Replies

4. Shell Programming and Scripting

Selective printing

I have the following contents in a file ---- CRITICAL: altered for /usr/bin/bin1 ---- OK: /usr/sbin/bin2 result fine ---- OK: /usr/sbin/bin3 result fine ---- CRITICAL: altered for /usr/bin/bin4 ---- OK: /usr/bin/bin5 result fine ---- OK: /usr/bin/bin6 result fine ---- CRITICAL: altered for... (9 Replies)
Discussion started by: anil510
9 Replies

5. Shell Programming and Scripting

selective printing

hi all from below text "abcd,SYS_12345,xyz,PQR, ," I want to print only "abcd,SYS,xyz,PQR, ," i.e. taking only first three 3 chars from 2 string of comma separated file thanks (4 Replies)
Discussion started by: JoeColeEPL9
4 Replies

6. Shell Programming and Scripting

Awk selective printing

Hi, i need help to print number from different field INPUT: Student1 10 20 Student2 30 40 Student3 50 60 Student4 70 80 Desired Output: 1 20-30 2 40-50 3 60-70 Thank you! (5 Replies)
Discussion started by: saint2006
5 Replies

7. Shell Programming and Scripting

sed selective printing

Hi, I have an xml file having serveral smiliar lines as below <INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="19" PHYSICALLENGTH ="15"... (3 Replies)
Discussion started by: dips_ag
3 Replies

8. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

9. Shell Programming and Scripting

selective printing of lines

Hi all , i need to grep for a string in a text file and print the string and the 3rd line above it. As always , Thanks. (4 Replies)
Discussion started by: okiedokie
4 Replies

10. Shell Programming and Scripting

selective printing awk

Hi there my file looks like this 1 a b c d e f 2 a b b c d e f f g h e t t 3 a c b d e f 4 a b c i want to print the line which has the fields containing ONLY a b c, in this case the line 4. How can i awk it !!!? Many Thanks in advance! (8 Replies)
Discussion started by: saint2006
8 Replies
Login or Register to Ask a Question