regexawkspecial-characters

add special characters "[" in print text using awk


this is a part of my bash script

..
mytstamp=$(date '+%Y-%m-%d %H:%M:%S :: ')
output=$(gawk -v mt="$mytstamp" -f print_errlog.awk errlog.txt)
..

my file: <errlog.txt>

2025-10-11 14:25:11 :: -----------------------------------------------------------------------------
2025-10-11 14:25:11 :: ERROR LOG FOR FILTER : AF7364-1322-20 200-4416AB/G3 A13 KII AF6086-005SP
2025-10-11 14:25:11 :: -----------------------------------------------------------------------------
2025-10-11 14:25:11 :: Err. 0011 - TOO MANY BLANK SPACES [ ] IN THE FILTER NAME
2025-10-11 14:25:11 :: Err. 0031 - WRONG INPUT FORMAT FOR FILTER NAME: [AF7364-1322-20 200-4416AB]/G3 A13 KII AF6086-005SP

my file: <print_errlog.awk>

BEGIN {}

{
  if ($0 ~ mt)
    gsub($0,mt,""); print >> "myerrlog.txt"
}

END {}

error of my bash script execution:

gawk: print_errlog.awk:8: (FILENAME=errlog.txt FNR=5) fatal: invalid regexp: Invalid range end: /2025-10-11 13:43:37 :: Err. 0031 - WRONG INPUT FORMAT FOR FILTER NAME: [AF7364-1322-20 200-4416AB]/G3 A13 KII AF6086-005SP/

So the problem causes only this opening square bracket "[" in the last line of <errlog.txt>. The closing square bracket "]" does not cause problems, if I delete or replace "[". Is there a way to have the square brackets in the code and printing them?

In the meantime I print it in this way without problems, but I do not like it:

if ($0 ~ mt)
  print substr($0,24,120) >> "myerrlog.txt"

Solution

  • You have provided the arguments for gsub in the wrong order.

    Searching $0 is the default, so your code can just be:

    mytstamp=$(date +'^%Y-%m-%d %H:%M:%S :: ')
    awk 'gsub(regex,"")' regex="$mytstamp" errlog.txt >> myerrlog.txt
    

    I added ^ so that the regex only matches at the start of the line.