How To Count Lines In A String Variable Using Perl

1 |
my $count = () = $string =~ /\n/g; |
![]() |
1 |
my $count = () = $string =~ /\n/g; |
1 2 3 4 5 |
my $needle = 'foo'; my $haystack = 'foo foo foo'; my @count = $haystack =~ /$needle/g; my $count = scalar @count; print "$count\n"; |
Should result in 3
The key is to define a variable first, then use the BEGIN block to initialize the variable, then reference the variable in use lib $var; later on ;-} For example, enable a module contained in the same directory as a script called via the PATH:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/ussr/bin/env perl use strict; use warnings; our $scriptPath; BEGIN { eval { use File::Basename; use File::Spec; $scriptPath = dirname(File::Spec->rel2abs(__FILE__)); 1; } or do { die "$0 use CRASHED: $@"; }; } use lib $scriptPath; eval { use MyModule::ABC; 1; } or do { die "$0 use CRASHED: $@"; }; |
The trick to using eval to catch shell execution is to return twice, once inside the eval and once outside:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
sub isMaintenance { my $return = eval { my $err=system("check_policy.sh | grep 'Policy is MAINTENANCE' >/dev/null 2>/dev/null"); &debug("perl error code: $err"); $err=$err / 256; &debug("system error code: $err"); if ($err != 0) { # Not Maintenance if ($verbose) { warn "policy is NOT MAINTENANCE\n"; } return 0; } else { return 1; } }; return $return; } |
Neat!
## to add a single backslash in front of whatever char was matched
1 2 3 4 |
my $password = q#abc'A1r%Fgt&jh[-#; $password =~ s/(['])/\\$1/g; print qq#'$password'\n#; 'abc\'A1r%Fgt&jh[-' |
## to double whatever char was matched
1 2 3 4 |
my $password = q#abc'A1r%Fgt&jh[-#; $password =~ s/(['])/$1$1/g; print qq#'$password'\n#; 'abc''A1r%Fgt&jh[-' |
## to convert ‘ to ‘\” for shell execution
1 2 3 4 |
my $password = q#abc'A1r%Fgt&jh[-#; $passwordShell =~ s/(['])/$1\\$1$1/g; print qq#'$password'\n#; 'abc'\''A1r%Fgt&jh[-' |
Problem While running the aws cli command from a Perl async command inside apid, I go the following error:
1 |
IOError: [Errno 10] No child processes |
Solution The issue turned out to be a bug in Python2.7, so I upgraded to Python3.4, then uninstalled and re-installed the aws cli software so that it used the proper Python34 version. Procedure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
sudo -i cd ## Upgrade Python yum install python34 alternatives --config python ## "Uninstall" old aws cli mv /opt/aws /opt/aws.fcs cd /usr/bin/ mv aws aws.fcs mv aws_completer aws_completer.fcs ## Install new aws cli curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip ./aws/install -i /opt/aws -b /usr/bin /usr/bin/aws --version |
https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html […]
1 2 3 4 5 6 7 8 9 10 11 12 |
my $stamp = strftime "%Y%m%d%H%M%S", gmtime time; ## in-place update for the file our $^I = ".$stamp"; local @ARGV = ($fullpath); while ( <ARGV> ) { if (/$regex/) { next; } # this print is the critical part to write the line back to the file print; } $^I = undef; |
1 2 3 |
use POSIX qw(strftime); my $stamp = strftime "%Y%m%d%H%M%S", localtime; my $stamp = strftime "%Y%m%d%H%M%S", gmtime; |
Using PERL, we can easily do a search and replace across multiple files. perl -ni -w -e ‘print unless m/^gdb\d+/;’ yourFileSpec
When using Perl’s exec() call, the best way to invoke it is as follows:
1 |
exec("$dir/myprog.sh",@ARGV); |
This syntax does some important things: – passes a scalar value as the first argument which exec interprets as PROGRAM – passes an array as the second argument which exec will use as the arguments to pass into PROGRAM Note […]