0

Here is my code. I wish to extract part of the text and write into another file. The loop of the code do not stop at my selected range of text. It read until the final match line of word. Please advise me. Thanks. For example, I need to extract the $ NAME: sandy until $$.TO and then join with the contents inside $NAME: patrick which is start from G1 until $$SRU.

TEXT:

$ NAME : corry  
$$.Inc s d
$$.Oc s
$$.TO

G1 ty n1 EE EE M T1 T2 $$SRU
G2 n1 y OO OO M T3 T4 $$SRU    
$$.EON

$ NAME : patrick    
$$.Inc c d
$$.Oc c
$$.TO

G1 td n3 EE EE M T5 T6 $$SRU      
G2 n3 y OO OO M T7 T8 $$SRU    
$$.EON
$ NAME : sandy    
$$.Inc k l
$$.Oc l
$$.TO

G1 td n3 FF FF M R5 R6 $$SRU      
G2 n3 y OO OO N R7 R8 $$SRU    
$$.EON

CODE:

use strict;
use warnings;

open my $F1, '<', 'testing.txt' or die "failed $!";
open my $F2, '>', 'out.txt' or die "failed $!";

while (<$F1>) {
if (/^\$ NAME : sandy/../\$.TO/) {
print $F2 $_;
}
if (/^\$ NAME : patrick/../\$.EON/) {
if(/^G1/../\$SRU){
 s/G1/G1.G1o.n/g;
print $F2 $_;}
}

 }
close $F1;
close $F2;
2
  • what piece of text do you need to extract? Commented Oct 14, 2013 at 6:24
  • Start from here->$ NAME : corry $$.Inc s d $$.Oc s $$.TO G1 ty n1 EE EE M T1 T2 $$SRU G2 n1 y OO OO M T3 T4 $$SRU $$.EON-<End here. Besides that,I would like to substitute G1 into G1.G1o.n before I write the output. Commented Oct 14, 2013 at 6:30

1 Answer 1

2

first of all there is not enought space before : in your regex, and you complicate your code...

use warnings;
use strict;

open my $fh, '<', 'in' || die "Can not open file:$!\n";;

while (<$fh>){
        print if /^\$ NAME : corry/../\$\$\.EON/;
}
close $fh;

If you need to write some data into other file first of all you need to open it for writing:

open my $fh2, '>>', 'my_out_file.txt'; #open file handler $fh2 associated with file named my_out_file.txt

then you can print to this file, like you print to the screen:

print $fh2 'some text here'; #print to file handler $fh2 string 'some text here'
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks! It works fine. Can you give me some idea on how I substitute G1 in the range of the selection text into G1.G1o.n and write the new text into another file?
Actually this text is some simple part from the whole text. G1.G1o.n refers to hierarchy organization. My perl skill is not standard yet so I'm thinking using rather old ways to substitute the G1 into G1.G1o.n
There are many small parts which make the final hierarchy section. the section corry and patrick are parts of the hierarchy. The overall hierarchy is under the section Manager which I din't post above.
May be because if my bad english or because of monday, but I dont realy understand what you want to do, sorry:D If you want to write line starting with G1 into another file you can do this while (<$fh>){if (//..//){print $fh2 $_ if /^G1/;}}
oh, is it mean while (<$fh>){if /^\$ NAME : corry/../\$\$\.EON/;{print $fh2 $_ if /^G1/;}}? Sorry, is it need to add something to create output file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.