Single quotes with AWK in Windows command shell

A standard awk script may look like this:

awk 'BEGIN { FS = "|"; OFS = "|"; ORS = "\n" } { sub(/88/, "+", $1) } { substr($1, 0, 4) } { substr($3, 0, 3) }{ print $2, $2, $3, $1 }' myFile.txt > out.txt

when you throw this into the Windows command line you get this:

awk: ^ invalid char ''' in expression


This is due to how cmd.exe interprets command line arguments. So, I added double quotes (") instead of single (') . That fails, because the next quote in the awk program ends the string (according to Windows). In order to make this string happy for Windows, you must "escape" the quotes inside the program by two quotes like this:

awk "BEGIN { FS = ""|""""; OFS = ""|""""; ORS = ""\n"""" } { sub(/88/, ""+"""", $1) } { substr($1, 0, 4) } { substr($3, 0, 3) }{ print $2, $2, $3, $1 }" small.txt > smallOut.txt

If you're keen, you'll notice that there are four quotation marks after each double quote to close a string. I'm not exactly sure why that works, but it does. If you only put two quotation marks to close a string, awk complains it's an infinite string.

Hope this helps.

Comments

  1. Thanks a lot . I use linux tools in a win environment and i always had the same awk fail (until now).You're my hero! thanks for share ¡

    ReplyDelete
  2. THANK YOU! Just for information in case someone has the same issue, I am using awk with CMAKE in the execute_process command and I was getting this error while trying to generate makefiles in a unix environment, oddly enough. Your article finally solved my problem.

    ReplyDelete
  3. Thank you, I was just wondering how to fix the "awk: ^ invalid char ''' in expression" message and Google drop me here.

    ReplyDelete
  4. Hmmm...This did not work for me on Windows 7. What did work was to escape the quotes in the expression with three quotes on each side, so

    awk 'BEGIN { FS = "|"; OFS = "|"; ORS = "\n" }

    becomes

    awk 'BEGIN { FS = """|"""; OFS = """|"""; ORS = """\n""" }.

    You may also need to escape the backslash with \\.

    ReplyDelete
  5. BIG THANK YOU, I was porting Linux shell script containing awk commands to windows and encountered invalid expression issue. But your post saved the day.

    ReplyDelete

Post a Comment

Popular Posts