Click to See Complete Forum and Search --> : grep and awk in perl


EscapeCharacter
01-26-2002, 02:27 AM
if i had the following text

HTTP/1.1 200 OK
Date: Sat, 26 Jan 2002 05:29:07 GMT
Server: Apache/1.3.22 (Unix) PHP/4.0.6
X-Powered-By: PHP/4.0.6
Connection: close
Content-Type: text/html

what would be the perl equiv to grep Server:|awk '{print $2}'? i suppose i could just put that in backticks in the perl code but thats taking the easy way out.
ive been working on it for about an hour and i see myself any close than i was an hour ago, any help would be appreciated.

EDIT:
just my luck five minutes after i post this i figure out the first part so ive got this so far.
@stuff = grep(/Server:/, split(/\n/, $line));


p.s. no Python is better comments(you know who you are)

[ 26 January 2002: Message edited by: EscapeCharacter ]

[ 26 January 2002: Message edited by: EscapeCharacter ]

takshaka
01-26-2002, 03:16 AM
Originally posted by EscapeCharacter:
@stuff = grep(/Server:/, split(/\n/, $line));

No need to do all that if the string is a scalar.

my $text = <<'TEXT';
HTTP/1.1 200 OK
Date: Sat, 26 Jan 2002 05:29:07 GMT
Server: Apache/1.3.22 (Unix) PHP/4.0.6
X-Powered-By: PHP/4.0.6
Connection: close
Content-Type: text/html
TEXT


my $server = $1 if $text =~ /^Server: (.*)/mi;
print "$server\n";

# or

($server) = $text =~ /^Server: (.*)/mi;
print "$server\n";

# if the text is in an array
my @array = split /\n/, $text;

$server = (split ' ', (grep /^Server:/i, @array)[0], 2)[1];
print "$server\n";

# that is

my @tmp = grep (/^Server:/i, @array);
$server = (split ' ', $tmp[0], 2)[1];
print "$server\n";

[ 26 January 2002: Message edited by: takshaka ]

EscapeCharacter
01-26-2002, 03:19 AM
cool thanks bro