user1769686
user1769686

Reputation: 535

endless loop in perl

I want to extract the content except http header from tcp flow files the content is like the following the http header ends when two ^M are met

HTTP/1.1 200 OK^M 
Last-Modified: Sat, 20 Mar 2010 09:43:12 GMT^M 
Content-Type: video/x-flv^M 
Date: Wed, 24 Oct 2012 14:34:13 GMT^M 
Expires: Wed, 24 Oct 2012 14:34:13 GMT^M 
Cache-Control: private, max-age=22124^M 
Accept-Ranges: bytes^M 
Content-Length: 29833281^M 
Connection: close^M 
X-Content-Type-Options: nosniff^M 
Server: gvs 1.0^M 
^M 
FLV^A^E^@^@^@   ^@^@^@^@^R^@^CK^@^@^@^@^@^@^@^B^@ 
onMetaData^H^@^@^@^O^@^Hduration^@@i<97> 
=p£×^@  starttime^@^@^@^@^@^@^@^@^@^@^Mtotalduration^@@i<97>

my code for extraction is as follows, and I run : extract.pl < tcp.flow but it seems the loop is endless, what is wrong with the codes? thanks!

#!/usr/bin/perl 
$start=0; 
$data=""; 
while(<STDIN>) 
{ 
    if ( $start eq 0 && $_ =~ /^\r\n/) { $start = 1; } 
    elsif ( $start eq 1 ) { $data = $data . $_; } 
} 
open(FH, ">sample.flv"); 
print FH $data; 
close(FH);

Upvotes: 1

Views: 297

Answers (2)

TLP
TLP

Reputation: 67900

This is a one-liner. I see no reason for any endless loop, however.

perl -00 -lne '$i++ and print' file > sample.flv

Which deparsed looks like this:

>perl -MO=Deparse -00 -lne '$i++ and print' input.txt
BEGIN { $/ = ""; $\ = "\n\n"; }        # from -l and -00
LINE: while (defined($_ = <ARGV>)) {   # from -n
    chomp $_;                          # from -l, removes "\n\n" now
    print $_ if $i++;                  # skips the first line
}
-e syntax OK

If you need to clean your file up first, just do

perl -pi -le 's/[\r\n]+$//' input.txt

Upvotes: 1

Aquatoad
Aquatoad

Reputation: 788

Call binmode() on STDIN before reading the data, it's possible that the contents of the file are interfering with the file reading. You'll want to use it on FH as well before writing the data. HTH

Upvotes: 1

Related Questions