Unix: Unexpected behavior of grep command with regex search -
i have grep command run daily find entry in huge logfile.
this command works fine in our development environment. in our production environment, outputs response different entry in logfile.
here's command:
logentry=$(grep -m1 -op '.*(?<=reset\s).*' $log)
actual entry in log file:
******reset counter:[total:1849766] [success:1849766] [insert:102] [update:1848861] [delete:803] [key:0]
command output:
******reset counter:[total:1849766] 1 [insert:102] [update:1848861] [delete:803] [key:0]
expected output:
******reset counter:[total:1849766] [success:1849766] [insert:102] [update:1848861] [delete:803] [key:0]
what reason behind inconsistent behavior of grep command?
thanks @ed morton comment. fix working fine.
root cause: variable not quoted it's open globbing, word-splitting, , filename expansion , net result dependent on files in directory.
solution: use echo "$logentry" instead , quote shell variabes unless have specific purpose in mind not doing , understand of implications.
security implications of forgetting quote variable in bash/posix shells
Comments
Post a Comment