copying more than one line to another file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cckramer
    New Member
    • Dec 2008
    • 11

    copying more than one line to another file

    When I read file A , it has these lines like that need to be reproduced:



    Input a, b, c,

    X,Y;



    Output m,n

    , f,g;



    How do I reproduce this in the new file ?

    Basically when I read “file A” line by line, I search for first word and if it’s “input” I need to start copying from there till the semicolon appears either in that line or the next line or so.



    For a 1 line statement I did the foll: I just need some pointers for a multiline statement.


    Code:
    while ($line = <$inFP>) {
    
    my (@tmp) = split (/\s+/, $line);
    
     if (($tmp[0] eq "output") && ($tmp[-1] =~ /;$/)) {
    
      print outFP "$line\n";
    
     }
    
    }
    Last edited by numberwhun; Dec 17 '08, 08:05 PM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    as posted on other forum:

    Code:
    while ($line = <DATA>) {  
       if ($line =~ /^(Input|Output)/) { 
          print $line; 
          while ($line = <DATA>) { 
             if ($line =~ /;\s*$/) { 
                print $line; 
                last; 
             } 
             print $line; 
          } 
       } 
    } 
    __DATA__ 
    Input a, b, c,  
    stuff here 
    X,Y;  
     
    test test 
     
    Output m,n  
    stuff here 
    , f,g; 
     
    test test
    if the semi-colon really is the last thing before the line ending you can remove \s* in this regexp:

    Code:
    if ($line =~ /;\s*$/) {

    Comment

    • cckramer
      New Member
      • Dec 2008
      • 11

      #3
      Appreciate the help offered.Also for trying to correct my ways in the other forum.

      Comment

      Working...