extracting a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jahanaz
    New Member
    • Nov 2008
    • 4

    extracting a string

    i have a input file of the format

    0000GH000
    0000GH000
    0000NM000

    How would i extract GHGHNM from this input file using perl scripts
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    What have you tried? You can do it using regular expressions.

    Comment

    • jahanaz
      New Member
      • Nov 2008
      • 4

      #3
      i am new to perl
      hardly a weeks experience

      i have tried this way

      if ($string =~ /^CPDS/)
      {
      if ((substr($strin g, 38, 2) ne " "))
      {
      @chk = $string;
      foreach $rec1 (@chk)
      {
      $chk1 = substr($rec1, 38, 2);
      }
      print "$chk1";
      }

      }
      this code prints my desired result on console
      but when it's printed on the file , i can see only the last 2 characters

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        The script you have posted has no relevance to the data that you posted initially. According to your initial posting, you were trying to extract two non-digits(alphabet s) from each line and appending them. the following code will do that:
        Code:
        use strict;
        open(DATA,"inputfile") or die "failed:$!";
        my $str="";
        while(<DATA>) {
        chomp;
        my $ch=$1 if(/(\D\D)/) ;  # extract consecutive non-digits
        print "$ch\n";
        $str.=$ch;   # append 
        }
        print "$str";
        If this does not solve your problem, please be more clear on what exactly you need.

        Comment

        • jahanaz
          New Member
          • Nov 2008
          • 4

          #5
          i tried this but this does not give my desired result .
          this is how it is supposed to work

          first it looks for records starting with CPDS
          then with in those CPDS records it checks whether position 38,2 is not null
          if it's true
          it has to get this 38,2 substring from all the choosen records
          so that each substring would be concatenated to become a single string.

          when i tried with ur code part it worked this way
          LK
          LKWN
          WNPR
          PR

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Originally posted by jahanaz
            i tried this but this does not give my desired result .
            this is how it is supposed to work

            first it looks for records starting with CPDS
            then with in those CPDS records it checks whether position 38,2 is not null
            if it's true
            it has to get this 38,2 substring from all the choosen records
            so that each substring would be concatenated to become a single string.

            when i tried with ur code part it worked this way
            LK
            LKWN
            WNPR
            PR
            Could you provide sample data from your file?

            Comment

            • jahanaz
              New Member
              • Nov 2008
              • 4

              #7
              am sorry my friend

              it worked
              I was using ur code a different way initially . This is just a small part of the whole code . i really had to integrate ur logic with my program's logic.

              anyway thanks a lot

              Comment

              Working...