Gossamer Forum
Home : General : Perl Programming :

retreiving selected data from a data file

Quote Reply
retreiving selected data from a data file
Hi,

iam a beginner in perl programming. i have a huge data file, whose format is as following:

r 0.210512sec 0 1 ack 64

- 0.210512sec 0 1 ack 64

r 0.26734sec 1 2 tcp 40

+ 0.43118sec 1 6 tcp 1540

r 0.43118sec 1 2 tcp 1540

---------------------and so on

my problems is as following :

1) iam interested in getting only those lines with " r " , " 1 " (in the 3rd column), "tcp" & "1540" in common while all those lines which donot have these four above stated characters should be deleted or ignored.

thanks in advance.

bye

-mariam Smile-

PS: I would highly appreciate if you would also refer me a script which i would modify according to my need.
Quote Reply
Re: [m_smith] retreiving selected data from a data file In reply to
you will need to change the regex if the fomat varies from what you gave.

Code:


my $datafile = '/home/you/data.txt';
my $newfile = '/home/you/good_data.txt';

open(IN, "< $datafile") || die $!;
open(OUT, "> $newfile") || die $!;
while(<IN>) {
/^r\s+[^\s]+\s+1\s+\d+\s+tcp\s+1540/ && print OUT; # or do whatever
}
close(IN) || die $!;
close(OUT) || die $!;
Quote Reply
Re: [adrockjames] retreiving selected data from a data file In reply to
Instead of [^\s]+ you can use \S+