Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a file data.txt having three columns. I want to print the rest of the column if the second column is same.
data.txt 1..1524 chromosomal replication initiator protein DnaA NC_017524 +.1 1..1524 chromosomal replication initiator protein DnaA NC_016768 +.1 1..1524 chromosomal replication initiator protein DnaA NC_009565 +.1 1000808..1002415 oxidoreductase NC_009565.1 1000808..1002415 oxidoreductase NC_017528.1 1002441..1002704 hypothetical protein NC_016934.1 1002812..1003792 peptidoglycan-binding protein ArfA NC_017523.1 1002812..1003792 peptidoglycan-binding protein ArfA NC_021194.1 1002812..1003792 peptidoglycan-binding protein ArfA NC_018143.2 1002812..1003792 peptidoglycan-binding protein ArfA NC_020089.1
I want the output as:
1..1524 chromosomal replication initiator protein DnaA NC_017524 +.1 NC_016768.1 NC_009565.1 1000808..1002415 oxidoreductase NC_009565.1 NC_017528.1 1002441..1002704 hypothetical protein NC_016934.1 1002812..1003792 peptidoglycan-binding protein ArfA NC_017523.1 + NC_021194.1 NC_018143.2 NC_020089.1
I will be thankful for any help

Replies are listed 'Best First'.
Re: match the second column and print rest of the column in horizontal form
by Corion (Patriarch) on Sep 28, 2016 at 09:23 UTC

    So what code have you already written and how does it fail to produce what you need?

Re: match the second column and print rest of the column in horizontal form
by tybalt89 (Monsignor) on Sep 28, 2016 at 13:31 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1172822 use strict; use warnings; $_ = do { local $/; <DATA> }; 1 while s/^\S+ +(\S.*?\S) .*\K\n.*? \1(?= )//m; print; __DATA__ 1..1524 chromosomal replication initiator protein DnaA NC_017524 +.1 1..1524 chromosomal replication initiator protein DnaA NC_016768 +.1 1..1524 chromosomal replication initiator protein DnaA NC_009565 +.1 1000808..1002415 oxidoreductase NC_009565.1 1000808..1002415 oxidoreductase NC_017528.1 1002441..1002704 hypothetical protein NC_016934.1 1002812..1003792 peptidoglycan-binding protein ArfA NC_017523.1 1002812..1003792 peptidoglycan-binding protein ArfA NC_021194.1 1002812..1003792 peptidoglycan-binding protein ArfA NC_018143.2 1002812..1003792 peptidoglycan-binding protein ArfA NC_020089.1