Skip to content

--extract-matches not as useful as --only-matching? #2

@martin-dorey-hv

Description

@martin-dorey-hv

I've been using --extract-matches quite a bit for some years and occasionally being bitten by an unexpected second match being catenated with the first one:

martind@stormy:~$ echo 'data-migrator-to-cloud-memory-limit-mbESC[36m is using the default value: 2048 (configure with the "set" command)' | regrep --extract-matches '[0-9]+'
362048
martind@stormy:~$ 

Someone else showed me that grep doesn't suffer from that infelicity:

martind@stormy:~$ echo 'data-migrator-to-cloud-memory-limit-mbESC[36m is using the default value: 2048 (configure with the "set" command)' | grep -o '[0-9]\+'
36
2048
martind@stormy:~$ 

I was then boggled to find that I could have been using that switch with regrep:

martind@stormy:~$ echo 'data-migrator-to-cloud-memory-limit-mbESC[36m is using the default value: 2048 (configure with the "set" command)' | regrep -o '[0-9]+'
36
2048
martind@stormy:~$ 

I see the implementations are adjacent and small:

 #elsif $$options{ExtMatches}
                    chomp (my $text = $_);
                    print $fhout $text =~ /$rx/g, "\n";
 #elsif $$options{OnlyMatching}
                    chomp (my $text = $_);
                    print $fhout $1, "\n"   while $text =~ /\G.*?($rx)/g;
 #else

... so I presume this is deliberate. I imagine that I stopped reading the help, likely up from the bottom, once I found the switch that did what I wanted:

    -y, --extract-matches
        Print only the parts of each line that your regex captures using
        brackets ( ).

... without ever spotting that there was another one to consider:

    -o, --only-matching
    -q, ...

Perhaps it'd be worth a few more words along the lines of "Each match is printed on a separate line, cf --extract-matches" and "Newlines are still printed to separate matches from different lines, cf --only-matching"?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions