Perl script to parse a text file and match a string -
i'm editing question add more details
the script executes command , redirects output text file.
the script parses text file match following string " standard 1.1.1.1"
the output in text file :
host configuration ------------------ profile hostname -------- --------- standard 1.1.1.1 standard 1.1.1.2
the code works if search either 1.1.1.1 or standard . when search standard 1.1.1.1 below script fails.
this error "unable find string: standard 172.25.44.241 @ testtest.pl
#!/usr/bin/perl use net::ssh::expect; use strict; use warnings; use autodie; open (hostrules, ">hostrules.txt") || die "could not open output file"; $hos = $ssh->exec(" typed command here "); print hostrules ($hos); close(hostrules); sub find_string { ($file, $string) = @_; open $fh, '<', $file; while (<$fh>) { return 1 if /\q$string/; } die "unable find string: $string"; } find_string('hostrules.txt', 'standard 1.1.1.1');
perhaps write function:
use strict; use warnings; use autodie; sub find_string { ($file, $string) = @_; open $fh, '<', $file; while (<$fh>) { return 1 if /\q$string/; } die "unable find string: $string"; } find_string('output.txt', 'object-cache enabled');
or slurp entire file:
use strict; use warnings; use autodie; $data = { open $fh, '<', 'output.txt'; local $/; <$fh>; }; die "unable find string" if $data !~ /object-cache enabled/;
Comments
Post a Comment