lundi 7 septembre 2015

How to find a value after a particular string and print it other file

I am matching a string in one file and want to print the value written after that string in certain file. Here is the code which I tried. It runs well but didn't produce any output

use strict;
use warnings;

open(my $file, "<", "abc.txt") || die ("Cannot open file.\n");
open(my $out, ">", "output.txt") || die ("Cannot open file.\n");

while(my $line =<$file>) {
chomp $line;
if ($line =~ /xh = (\d+)/) {
print $out $_;
}
}



abc.txt
a = 1 b = 2 c = 3 d = 4 
+xh = 10 e = 9 f = 11
+some lines
+xh = 12 g=14
+some lines
some lines
+xh = 13 i=15 j=20
some lines   


output.txt
10
12
13

Please suggest to improve my code. There is a "+" sign before every xh, and there is a white space before and after every "=" sign. I need to print every value of xh in other file. There is a "+" sign at the beginning of few lines. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire