Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semna
    Member
    • Apr 2010
    • 55

    #1

    regular expression in perl?

    Hi,
    I have a list contains:
    ID : 741158
    PARENT ID : 9605
    RANK : species
    GC ID : 1
    MGC ID : 2
    SCIENTIFIC NAME : Homo sp. Altai
    GENBANK COMMON NAME : Denisova hominin
    //
    In some part of this file the rank is subspecies. I just need those ID line (first line) which belong to species not subspecies:
    I used this : my @new_list = grep {$_ =~ /\bspecies$/ && /\nID +\: *([\d]+)/} @list;
    but is not working. Any suggestion? Thanks
  • steven
    Senior Member
    • Aug 2009
    • 269

    #2
    cat file | awk '$1=="ID"{id=$0} $1=="RANK" && $3=="species"{print id}'

    Assuming an ID line is provided for each entry.
    If you just need the ID number, use "id=$3" instead of "id=$0".

    Comment

    • semna
      Member
      • Apr 2010
      • 55

      #3
      Thanks Steven. But I am not sure that it works in perl .

      Comment

      • gringer
        David Eccles (gringer)
        • May 2011
        • 845

        #4
        So, you asked about perl. Here's a one-liner that roughly matches steven's awk script:
        Code:
        $ perl -ne 'if(/ID *: *(\d+)/){$id=$1};if(/RANK *: *species/){print "$id\n"}' file.txt
        9605
        If you're using it inside some other perl code, you'd probably do something like this:
        Code:
        my @new_list = ();
        my $id = "";
        # this assumes you want to store IDs from files or standard in
        while(<>){
          if(/^ID *: *(\d+)/){$id=$1};
          if($id && /^RANK *: *species/){
            push(@new_list, $id);
          }
        }
        Or if you're reading from the array @list, which contains one element per line:
        Code:
        my @new_list = ();
        my $id = "";
        foreach(@list){
          if(/^ID *: *(\d+)/){$id=$1};
          if($id && /^RANK *: *species/){
            push(@new_list, $id);
          }
        Or, assuming your example is exactly as your code looks (i.e. each element in @list contains a number of lines, but only one record per list element):
        Code:
        my @new_list = ();
        foreach (@list){
          if(/^RANK *: *species/){
            if(/^ID *: *(\d+)/){
              push(@new_list, $1);
            }
          }
        }
        [but if that were the case, your code would probably work, but would spit out the entire record, rather than just the ID]


        I would advise you to stay away from using grep in situations like this where you're modifying things inside a loop. It would do weird things like not adding to your result array if $id were 0, and changing $_ would alter your original list. See here for more information:

        Comment

        • steven
          Senior Member
          • Aug 2009
          • 269

          #5
          Yes, that is not perl indeed but awk. If you really need to include this in a bigger perl code, the equivalent can be obtained by splitting the lines and adding a couple of "if" and "else". And I bet the same can be obtained by just using linux command lines, directly from the shell.

          Comment

          • steven
            Senior Member
            • Aug 2009
            • 269

            #6
            Nice job gringer, that is a complete answer

            Comment

            • gringer
              David Eccles (gringer)
              • May 2011
              • 845

              #7
              I bet the same can be obtained by just using linux command lines, directly from the shell
              I think an awk one-liner is close enough to 'linux commands, directly from the shell'. A command-line equivalent using multiple grep pipes would go something like this:
              $ grep '^\(ID\|RANK\)' file.txt | grep -B 1 '^RANK : species' | grep -v '^RANK'
              ID : 741158
              Although you'd run into problems with that if any IDs didn't have associated ranks following them.

              Comment

              • semna
                Member
                • Apr 2010
                • 55

                #8
                Thanks so much Gringer and Steven.

                Comment

                Latest Articles

                Collapse

                • SEQadmin2
                  Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
                  by SEQadmin2



                  CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

                  Despite this, “CRISPR helped turn genome editing from a specialized technique into
                  ...
                  07-31-2026, 11:01 AM
                • SEQadmin2
                  Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
                  by SEQadmin2


                  Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

                  The systematic characterization of the human proteome has
                  ...
                  07-20-2026, 11:48 AM

                ad_right_rmr

                Collapse

                News

                Collapse

                Topics Statistics Last Post
                Started by SEQadmin2, Today, 10:35 AM
                0 responses
                6 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 08-06-2026, 07:41 AM
                0 responses
                23 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 08-03-2026, 10:13 AM
                0 responses
                41 views
                0 reactions
                Last Post SEQadmin2  
                Started by SEQadmin2, 07-31-2026, 02:55 AM
                0 responses
                47 views
                0 reactions
                Last Post SEQadmin2  
                Working...