ack : get the 10th (or bigger nth) matching/capturing group
Here's a possible hack, though I am not a perl expert. Looking at the all-in-one source file, it seems that ack
is made to handle only a single character after $
in the output string. Changing this to accept multiple characters is no doubt feasible, but to keep hacks simple, you can extend 0..9
with abc...
. For example, I made these changes to accept $a
and $b
as $10
and $11
(shown as a diff -u
)
@@ -188,7 +188,7 @@
$opt_output =~ s/\\r/\r/g;
$opt_output =~ s/\\t/\t/g;
- my @supported_special_variables = ( 1..9, qw( _ . ` & ' + f ) );
+ my @supported_special_variables = ( 1..9, qw( a b _ . ` & ' + f ) );
@special_vars_used_by_opt_output = grep { $opt_output =~ /\$$_/ } @supported_special_variables;
# If the $opt_output contains $&, $` or $', those vars won't be
@@ -924,6 +924,8 @@
# on them not changing in the process of doing the s///.
my %keep = map { ($_ => ${$_} // '') } @special_vars_used_by_opt_output;
+ $keep{a} = $10;
+ $keep{b} = $11;
$keep{_} = $line if exists $keep{_}; # Manually set it because $_ gets reset in a map.
$keep{f} = $filename if exists $keep{f};
my $special_vars_used_by_opt_output = join( '', @special_vars_used_by_opt_output );
If you just want to go up to the 10th match, however, you can use $+
as it shows the text matched by the last bracket of the last successful search pattern.