Data extraction in Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • v2ksweet
    New Member
    • May 2013
    • 1

    Data extraction in Perl

    I have a below line in file '/data/control.dat'..

    migration.MW051 52013/nm16,CDW

    I want to fetch only migration.MW051 52013/nm16

    Below is the code, But it is fetching full line. Can you help on this.

    Code:
    $CONTROL_FILE=/data/control.dat
            open (CONTROL_FILE, "<$CONTROL_FILE") or handle_error(open_control_file);
    
            while ( $ENTRY = <CONTROL_FILE> )
            {
                    if ( $ENTRY =~ /^(.*?)\s/ )
                    {
    Last edited by Rabbit; May 15 '13, 08:39 PM. Reason: Please use code tags when posting code.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Try changing your regex to this:
    Code:
    /^([^/]+)/

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      v2k,

      Are you trying to read a partial line, or extract the desired information at line 6 of your code sample?

      [RonB] I think he wants to break at the comma, not a stroke.

      This might do what you need, if you insert it above line 6:
      Code:
      ($ENTRY,$rest) = ($ENTRY =~ m/([^,]*),(.*)/);
      Cheers,
      Oralloy

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Oralloy, you're right. I need to start wearing my glasses. ;o)

        I'd probably end up using split.

        Code:
        my $CONTROL_FILE = '/data/control.dat';
        
        open my $ctrl_fh, '<', $CONTROL_FILE or handle_error("open_control_file <$!>");
        
        while ( my $line = <$ctrl_fh> ) {
        
            my $entry = (split(/,/, $line))[0];    
            # do whatever is needed with $entry 
        
        }

        Comment

        Working...