Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep and Awk in Windows Invalid Char in Expression Error

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

I am have having trouble running this script:

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

I get the error:

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

I believe it might have something to do with having to use double quotes in Windows, but I tried all the combinations I can think of and still it doesn't work.

Can anyone help? Thanks

like image 202
Nathan Avatar asked Jan 31 '11 14:01

Nathan


1 Answers

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

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

needs to be changed to

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

But remember, you can't use double quotes inside double quotes, you need to escape them. On Windows you can't simply use \ to escape it. You need the following syntax:

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

Right, that's \"" to represent " inside double quotes.

like image 182
Just a learner Avatar answered Sep 21 '22 08:09

Just a learner