Perl Notes
Jump to navigation
Jump to search
find and capture in one line
my ($found) = ($text =~ /pre (capture stuff in brackets) post/)
Note the brackets around $found - match list is converted to a number for a scalar
sort
@sorted = sort @array; Numerical: @sorted = sort { $a <=> $b } @notsorted; Alphabetical sort: @sorted = sort { lc($a) cmp lc($b) } @notsorted; Alphabetical sort, reversed: @sorted = sort { lc($b) cmp lc($a) } @notsorted;
join log lines so we can grep
e.g. log4j where each line starts with 2014-12-01 but stack traces/multi line buffers don't
perl -ne '$_ =~ s/\n//; if ($_ =~ /^\d{4}-\d{2}-\d{2}/) { print "\n$_"; } else { print "|$_"; }'
join exceptions in java logs
perl -e 'my $l = ""; while (<>) { if ($_ !~ /^\d+/) {chop($l); $l.=$_;} else { print $l; $l=$_;}}'