By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,775 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,775 IT Pros & Developers. It's quick & easy.

copying more than one line to another file

P: 11
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.


Expand|Select|Wrap|Line Numbers
  1. while ($line = <$inFP>) {
  2.  
  3. my (@tmp) = split (/\s+/, $line);
  4.  
  5.  if (($tmp[0] eq "output") && ($tmp[-1] =~ /;$/)) {
  6.  
  7.   print outFP "$line\n";
  8.  
  9.  }
  10.  
  11. }
  12.  
Dec 17 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
as posted on other forum:

Expand|Select|Wrap|Line Numbers
  1. while ($line = <DATA>) {  
  2.    if ($line =~ /^(Input|Output)/) { 
  3.       print $line; 
  4.       while ($line = <DATA>) { 
  5.          if ($line =~ /;\s*$/) { 
  6.             print $line; 
  7.             last; 
  8.          } 
  9.          print $line; 
  10.       } 
  11.    } 
  12. __DATA__ 
  13. Input a, b, c,  
  14. stuff here 
  15. X,Y;  
  16.  
  17. test test 
  18.  
  19. Output m,n  
  20. stuff here 
  21. , f,g; 
  22.  
  23. test test
  24.  
if the semi-colon really is the last thing before the line ending you can remove \s* in this regexp:

Expand|Select|Wrap|Line Numbers
  1. if ($line =~ /;\s*$/) {
Dec 17 '08 #2

P: 11
Appreciate the help offered.Also for trying to correct my ways in the other forum.
Dec 18 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.