Grep and Awk on Windows Invalid Char in Error expression

I am new to grep and awk - using Windows 7 (I downloaded grep and awk for windows from GnuWin).

I am having trouble running this script:

grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}' 

I get an error:

awk: '{print
awk: ^ invalid char '' 'in expression

I believe this may be due to the need to use double quotes in Windows, but I tried all the combinations that I can think of, and yet it does not work.

Can anyone help? Thanks

+7
source share
7 answers

Removing command line elements is always a pain in Windows. As a last resort, you could use gawk -f !

So: your script.awk file contains:

 print $2,$1 

And you do grep -Fwf dictionary.txt frequency.txt | awk -f script.awk grep -Fwf dictionary.txt frequency.txt | awk -f script.awk

+4
source

On Windows, you need to use double quotes to indicate your awk commands. So,

 grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}' 

need to change to

 grep -Fwf dictionary.txt frequency.txt | awk "{print $2 "," $1}" 

But remember, you cannot use double quotes inside double quotes, you need to avoid them. On Windows, you cannot just use \ to avoid this. You will need the following syntax:

 grep -Fwf dictionary.txt frequency.txt | awk "{print $2 \"",\"" $1}" 

It is correct that \ "" represents " inside double quotes.

+20
source

You need to use double quotes around your awk script and escape the inline quotes in the print statement using a nice old backslash: [g] awk "BEGIN {print \" Hello escape char! \ "}"

+4
source

Here is a short example that will take input.csv and then output new.csv :

 gawk < input.csv -F, "{print $1 \"",\"" $5} ">new.csv 
+1
source

Because the brackets must be in double quotation marks, three sets of double quotation marks are required inside a square expression.

For example:

gawk "{print $2 """,""" $1}"

+1
source

I staked out over the years for AWK to work under the windows. There are problems with citation and route restrictions. My final solution is to β€œlet AWK fly for free,” which is command line free. I understand that it was designed as glue for the unix style juju command line, but I just wanted to use it as a scripting language.

All of my AWK scripts contain a list of goals and a specific output file. They can be launched by double-clicking on the associated DOS batch file:

 : AWK.BAT - place in the same directory as GAWK @echo off :Check %1 in not null If [%1]==[] ( cls Echo No parameters passed goto End ) : Change to the parameter file location cd /D "%~dp1" : Set PrintFile - this will be the name of the script (not the target file) with ".out" Set PrintFile=%~nx1.out :Run AWK : -v PrintFile to allow renaming of output file : -f ScriptFile.awk the program : > Redirects output to a known destination cls P:\MyPrograms\EDITORS\Addins\gawk\gawk.exe -v PrintFile=%PrintFile% -f %* >%PrintFile% :End pause 

The following is an example of my AWK scripts (extract all the lines using the :: tab and print them):

 # AWK Template BEGIN{ ## Hard Code Target Files - Unix paths with / separators ## # Realtive paths from the location of ScriptFileName.awk # These will be added to the end of the ARG array - after any command line target files AddTarget("../APEdit.ahk") ## Hard Code Output Files - WinDos paths with \\ separators ## # Realtive paths from the location of ScriptFileName.awk # Default is ScriptFileName.awk.out passed in as a variable called PrintFile # PrintFile will be copied to OutputFile after processing using the END section OutputFile = "Keys.txt" # Set input record sep and field sep RS="\n" FS=" " # Set output RS and FS ORS="\n" OFS=" " # Write a header print "Key assignments from the source code" print " " } ## MIDDLE - Once per matching record! ## # Find autohotkey key definitions /::\t/ { print $0 } END{ ## Rename output files if (OutputFile) { system("Copy /Y " PrintFile " " OutputFile) } } ## Functions ## function AddTarget(FN){ # Need to check file exists if (FileExists(FN)){ ARGV[ARGC] = FN ARGC ++ } } function FileExists(FN) { if ((getline < FN) > 0) { close(FN); return 1 } else { print "Target file not found " FN > "error.awk.txt" return "" } } 

You can see that this defines the purpose of the input in the script and determines the final purpose of the output in the script. It uses a temporary β€œ.out” file to avoid large print redirection by copying the file to the desired output in the END script section.

I have associated AWK files with this batch file and an option has been added in my editor to send AWK files to a batch file.

Yours faithfully

0
source

Yusui said:

... you cannot use double quotes inside double quotes, you need to avoid them. On Windows, you cannot just use \ to avoid this.

while ReluctantBIOSGuy used only "in his own example."

I tried both "and" and both, and it works for me (gawk on Windows XP both on the command line and in the batch file).

Here is an example that includes "in output (string literal in c code):

 FCIV\fciv -add ..\07-Sources -type *.h -type *.c -bp ..\07-Sources | find /V "//" | sort /+33 > listof.md5 FCIV\fciv -add listof.md5 | find /V "//" | gawk "{print \"static const char md5[] = \\\"\" $1 \"\\\";\"}" > ..\07-Sources\generated\md5.c 
0
source

All Articles