{"id":24,"date":"2021-01-12T08:42:47","date_gmt":"2021-01-12T08:42:47","guid":{"rendered":"https:\/\/veltzer.net:8890\/?p=24"},"modified":"2021-01-12T08:44:03","modified_gmt":"2021-01-12T08:44:03","slug":"grep-is-broken-use-perl-instead","status":"publish","type":"post","link":"https:\/\/veltzer.net:8890\/grep-is-broken-use-perl-instead\/","title":{"rendered":"Grep is broken \u2013 use perl instead"},"content":{"rendered":"\n

In the course of running various grep(1) combination to find various defects in source files I ran into inherent grep(1) difficulties. It seems that the regular expression syntax in grep(1) is very limited and so it\u2019s better to use perl to solve these issues as it\u2019s regular expression support is fantastic. A small script can solve all your grepping needs. So here it is. Please comment with fixes and I\u2019ll incorporate them if you want to add features.<\/p>\n\n\n\n

#!\/usr\/bin\/perl -w\n \n# This is a general script to grep using perl to overcome some of the deficiencies\n# of grep(1) grepping...\n \nuse strict;\nuse diagnostics;\n \nmy($pattern)=$ARGV[0];\nmy($debug)=0;\nmy($print_filename)=1;\n \nfor(my($i)=1;$i<@ARGV;$i++) {\n        my($filename)=$ARGV[$i];\n        if($debug) {\n                print \"filename is [$filename]\\n\";\n        }\n        open(FILE,$filename) || die \"Can't open file [$filename]: $!\";\n        my($line);\n        while($line=<FILE>) {\n                if($line=~$pattern) {\n                        if($print_filename) {\n                                print $filename.\": \";\n                        }\n                        print $line;\n                }\n        }\n        close(FILE) || die(\"unable to close file [$filename]: $!\");\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

In the course of running various grep(1) combination to find various defects in source files I ran into inherent grep(1) difficulties. It seems that the regular expression syntax in grep(1) is very limited and so it\u2019s better to use perl to solve these issues as it\u2019s regular expression support is fantastic. A small script can … <\/p>\n