Re: How do i extract 3 variables from each line in a file, and print them to a new file
by monarch (Priest) on Jul 08, 2005 at 16:14 UTC
|
If you just want the last 3 numbers in a line you can use a regular expression: my $line = "3434 34456... 321ms:543ms:45ms";
if ( $line =~ m/(\d+)\D+(\d+)\D+(\d+)\D+$/ )
{
print( "$1,$2,$3\n" );
}
| [reply] [d/l] |
Re: How do i extract 3 variables from each line in a file, and print them to a new file
by davidrw (Prior) on Jul 08, 2005 at 16:16 UTC
|
you'll probably see a bunch of different ways, including split .. here's one pure regex way:
open OUTFILE, ">out.txt";
open INFILE, "in.txt";
while(<INFILE>){
next unless /(\d{1,3})ms:(\d{1,3})ms:(\d{1,3})ms/;
print OUTFILE join(",", ($1, $2, $3) ), "\n";
}
close OUTFILE;
close INFILE;
| [reply] [d/l] [select] |
|
That one is an almost complete solution... Mine is shorter; this one should give the closest match... Add "$" at the end of your regex and it will be perfect...
--------------------------------
An idea is not responsible for the people who believe in it...
| [reply] |
|
your solution didn't work ...
Yeah, i guess i could anchor to the end (especially since OP said 'last 3 numbers' but might be able to assume that the ":ms" strings in there will be restrictive enought to get the right one.. i guess to go strictly by 'last 3 numbers' the (\d+)\D+ solution above is better (though in that one i would make the last \D+ a \D*)..
| [reply] [d/l] [select] |
|
|
Re: How do i extract 3 variables from each line in a file, and print them to a new file
by bofh_of_oz (Hermit) on Jul 08, 2005 at 16:14 UTC
|
I believe this line should do it:
$_ =~ /(\d+)ms/g;
print "$1,$2,$3\n";
UPDATE: I agree with comments since I've got the same result... Here's what worked:
$_ = "3434 34456... 321ms:543ms:45ms";
($d, $e, $f) = ($_ =~ /(\d+)ms/g);
print "$d,$e,$f\n";
--------------------------------
An idea is not responsible for the people who believe in it...
| [reply] [d/l] [select] |
|
my @nums = $_ =~ /(\d+)ms/g;
if you want to capture all matches from a /g.
| [reply] [d/l] [select] |
|
perl -le '$_="123ms456ms789ms"; $_ =~ /(\d+)ms/g; print "$1,$2,$3\n";
+'
prints just 123,, | [reply] [d/l] [select] |
Re: How do i extract 3 variables from each line in a file, and print them to a new file
by Transient (Hermit) on Jul 08, 2005 at 16:56 UTC
|
TMTOWTDI - and it's usually wrong silly...
#!/usr/bin/perl
use warnings;
use strict;
open( OUTPUT, ">output" ) or die "Unable to open output\n$!\n";
$,=","; $\="\n";
while (<DATA>) {
print OUTPUT map { $_ =~ s/\D//g; $_ } split(':',(split(' ',$_))[-1])
+;
}
close OUTPUT or die "Close output failed\n$!\n";
__DATA__
3434 34456 7788 9999 65444 4 444 444 44443225 12ms:233ms:455ms
755655 5789 333 666776 5553 353534 33 321ms:543ms:45ms
Update: ...and the finale:
perl -pe '$_=join(",",map{s/\D//g;$_} split(":",(split(" ",$_))[-1])).
+"\n"' < input > output
| [reply] [d/l] [select] |
|
Check this Data.dat contains the input lines. This is working.
open(Spooler, "Data.dat") or die "File does not exists\n";
while($spooler=<Spooler>) {
#print "$spooler\n";
chomp($spooler);
@matches = $spooler =~ /(\d+)ms/g;
foreach $a (@matches){
print $a .",";
}
print "\n";
}
Hope it helps!
| [reply] [d/l] |
|
That would print a comma at the end of the output string, so you would need a way to prevent it, let's say, like this:
for (@matches)
{
print "$_,";
}
print "\b ";
(Sorry about replacing foreach with for - someone mentioned to me earlier that it's faster and works the same way anyways)
Personally, I prefer Transient's way of doing things... although I don't quite understand why it parses split first and then map...
--------------------------------
An idea is not responsible for the people who believe in it...
| [reply] [d/l] |
|
|
|
|
@matches = $spooler =~ /(\d+)ms/g;
foreach $a (@matches){
print $a .",";
}
print "\n";
how about
print join(',',$spooler=~/(\d+)ms/g)."\n";
| [reply] [d/l] [select] |
|
Thanks, that worked great:)
| [reply] |
Re: How do i extract 3 variables from each line in a file, and print them to a new file
by bageler (Hermit) on Jul 08, 2005 at 18:02 UTC
|
Here is a solution without using any regular expressions:
$_="22 34808 934 20987129 83454 34643 12ms:34ms:56ms";
@f = unpack("A2A2A3A2A3A2",reverse $_);
print join(',',map{join'',scalar reverse}@f[5,3,1]);
Update: didn't read the problem fully. This is an incomplete solution. | [reply] [d/l] |