[SOLVED] Perl: Highlight the text that Matches the Regex
by blueray from LinuxQuestions.org on (#527J2)
When I use ag or rg to select a text, it highlight the selected text.
Code:$ echo "First HTML appeared, then CSS, then JavaScript" | rg -N 'HTML|php|CSS|Java(Script)?'
$ echo "First HTML appeared, then CSS, then JavaScript" | ag --nonumbers 'HTML|php|CSS|Java(Script)?'But when I use Perl, it does not highlight the matching text.
Code:$ echo "First HTML appeared, then CSS, then JavaScript" | perl -ne '/HTML|php|CSS|Java(Script)?/ and print'I am attaching am image to illustrate my point.
I tried the following:
Code:echo "First HTML appeared, then CSS, then JavaScript" | perl -ne 'use Term::ANSIColor qw(:constants);/HTML|php|CSS|Java(Script)?/ and print RED, $_'
echo "First HTML appeared, then CSS, then JavaScript" | perl -ne 'use Term::ANSIColor;/HTML|php|CSS|Java(Script)?/ and print color("red"), $_'It changes the color of the whole text and not the selected text.
How can I highlight the text that matches the Regex when using Perl to search?
Attached Thumbnails


Code:$ echo "First HTML appeared, then CSS, then JavaScript" | rg -N 'HTML|php|CSS|Java(Script)?'
$ echo "First HTML appeared, then CSS, then JavaScript" | ag --nonumbers 'HTML|php|CSS|Java(Script)?'But when I use Perl, it does not highlight the matching text.
Code:$ echo "First HTML appeared, then CSS, then JavaScript" | perl -ne '/HTML|php|CSS|Java(Script)?/ and print'I am attaching am image to illustrate my point.
I tried the following:
Code:echo "First HTML appeared, then CSS, then JavaScript" | perl -ne 'use Term::ANSIColor qw(:constants);/HTML|php|CSS|Java(Script)?/ and print RED, $_'
echo "First HTML appeared, then CSS, then JavaScript" | perl -ne 'use Term::ANSIColor;/HTML|php|CSS|Java(Script)?/ and print color("red"), $_'It changes the color of the whole text and not the selected text.
How can I highlight the text that matches the Regex when using Perl to search?
Attached Thumbnails