![]() Starting with Perl 5.20, List::Util 1.33 should be bundled and should therefore work without the need of installing an additional module. grep -v 'unwantedword' file grep XXXXXXXX grep -v 'unwantedword' file will filter the lines that have the unwantedword and grep XXXXXXXX will list only lines with pattern XXXXXXXX. "any" is not exported by the List::Util moduleĬan't continue after import errors at any.pl line 2.īEGIN failed-compilation aborted at any.pl line 2. The Linux grep command is a useful tool for string and pattern matching, allowing you to search through text files using various options. You can do it using -v (for -invert-match) option of grep as. On a RHEL 7 machine with Perl 5.16 and without manually updated modules using CPAN you will most likely run into such an /tmp $ perl any.pl "Error" However this only works if you have at least version 1.33 of the List::Util Perl module installed. Input (Error) matched against our string /tmp $ perl any.pl "Error, test"Īccording to a benchmark, using 'any' is much faster than 'grep'. The same exact string comparison and matching works /tmp $ perl any.pl "Error" The following code can be /tmp $ cat any.pl "Error occurred"Īlternative exact string comparison using 'any'Īs pointed out in comments on my Mastodon toot, another alternative would be to use the 'any' function, which is part of List::Util module. An input which contains (but not exactly matches) an entry of the should not trigger a /tmp $. "Error"Īnd (important to the use of this script) is that only an exact match works. The Perl script now returns whether or not the input string matches one of the /tmp $. Print "Input ($input) matched against our string list\n" Īccording to the Perl grep documentation, the $_ variable represents each entry of the list:Įvaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to true. ![]() To grep for an exact match (eq) compared to a list of array, the following (simplified) code was /tmp $ cat When the input exactly matches against one of the array items, then print that a match was found.įor this purpose, the Perl-internal grep function (similar in behaviour to the grep Shell command, but not exactly the same) can be used. Inside a Perl script I wanted to compare an input string ($ARGV) against an array of strings. Published on August 4th 2023 - last updated on August 7th 2023 - Listed in Perl Coding How to grep for an exact string match with Perl
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |