Trimming text in perl -
Trimming text in perl -
i have file has text formatted that:
timeticks: (243749689) 28 days, 5:04:56.89 oid: .1.3.6.1 string: "07/10/14 - 16:36:50" hex-string: 09 15 integer: 2 string: "str:/storage0/alarm/process/2/sentence xxxx" string: "storage0" string: "23250002" string: "4039209" integer: 0 ipaddress: 172.17.1.41 string: "public" oid: .1.3.6.1
i trying trim info can't done.
i need info trimmed that:
date: 07/10/14 - 16:36:50 process: process priority: 2 sentence: sentence xxxx comunity: public
i trying star that:
foreach $x (@varbinds) { next if ($x =~ /^timeticks:/); next if ($x =~ /^oid:/); next if ($x =~ /^hex-string:/); printf output_file "%s\n", $x->[1]; }
can give me hand?
update
this structure
my (@varbinds) = @{$_[1]}; unless (sysopen(output_file, $snmptrapperfile, o_wronly|o_append|o_creat, 0666)) foreach $x (@varbinds) { next if ($x->[1] =~ m/timeticks/); next if ($x->[1] =~ m/integer/); if ($x->[1] =~ /^string/) { printf output_file "%s\n", $x->[1]; } } close (output_file);
correct, , giving me output:
08/10/14 - 12:10:20 str:/storage0/alarm/process/2/sentence xxxx" storage0 23250002 4065151 public
using construction of foreach, how format?
here's solution reads of info data
file handle , processes single chunk. expects find single set of info in file, , lines of involvement first, second, , 6th lines begin string:
. subsidiary info taken last three fields of second such line when split on slash characters /
.
if wish read specific file specified on command line, should read using <>
instead of <data>
. if want hard-code path file need create phone call open
, this
open $fh, '<', 'myfile';
and read using file handle open
creates, using <$fh>
instead of <data>
.
use strict; utilize warnings; utilize 5.010; $data = { local $/; <data>; }; @strings = $data =~ /^string\s*:\s*"([^"]*)/mg; @fields = split qr|/|, $strings[1]; printf "date: %s\n", $strings[0]; printf "process: %s\n", $fields[-3]; printf "priority: %s\n", $fields[-2]; printf "sentence: %s\n", $fields[-1]; printf "community: %s\n", $strings[5]; __data__ timeticks: (243749689) 28 days, 5:04:56.89 oid: .1.3.6.1 string: "07/10/14 - 16:36:50" hex-string: 09 15 integer: 2 string: "str:/storage0/alarm/process/2/sentence xxxx" string: "storage0" string: "23250002" string: "4039209" integer: 0 ipaddress: 172.17.1.41 string: "public" oid: .1.3.6.1
output
date: 07/10/14 - 16:36:50 process: process priority: 2 sentence: sentence xxxx comunity: public
perl
Comments
Post a Comment