How to run the awk command in Windows 7?

1

I have the following adb + awk command which will work fine in Linux environment [Ref].

adb shell dumpsys package | awk -v RS='\n +Package' '/android\.permission\.CAMERA/{print $1}'

But I need to run this command in Windows 7 PC which has GnuWin32 Gawk package installed. I can run awk commands from the C:\Program Files (x86)\GnuWin32\bin folder and run the adb commands from the C:\Program Files (x86)\Android\android-sdk\platform-tools folder. I need to run the above command and get the list of packages that has the CAMERA permissions allowed.

Setting Windows Environmental Variables for AWKPATH and also in PATH variables didn't work. So I just copied the contents of GnuWin32 gawk's bin folder and pasted in the platform-tools folder. But when I run in command prompt I get,

awk: +Package'
awk:         ^ invalid char ''' in expression

How can I run the above command in Windows? or What is the correct expression that I can run?

Lucky

Posted 2015-09-15T10:57:50.593

Reputation: 422

Try to replace the ' with " : http://stackoverflow.com/questions/4852270/grep-and-awk-in-windows-invalid-char-in-expression-error

– duDE – 2015-09-15T11:17:57.583

Really you should have tried a bit of troubleshooting. e.g. Try just using Gawk for a simple case and see if you get that error..then it may be clearer to you what the problem is – barlop – 2015-09-15T11:19:52.603

@barlop I'm new to gawk and simple examples works fine. I only had problems with case including the '. But that is solved now according to the user duDE mentioned. – Lucky – 2015-09-15T11:25:49.717

1@duDE that solved the issue. Thanks. Could you post that as an answer. – Lucky – 2015-09-15T11:26:10.700

1@Lucky The point is that troubleshooting involves looking at what doesn't work and looking at what works and changing each to pinpoint the error. e.g. trying to change the one that doesn't work into something simpler. So you could have tried simplifying the left hand side to echo asdf, and you'd see you still get the error, then you could have done echo asdf | awk -v RS='b' '{print $1}' and got that error. You still get the error but it's much easier for you or anybody to diagnose.. nothing to do with adb or android for example, it rules that out. – barlop – 2015-09-15T12:01:45.500

@barlop Okay, thanks for the advice. As I mentioned already I'm just new to using gawk commands as I'm a Windows user and I didn't knew it had nothing to do with adb command. In the end, it was just a simple mistake. ;) – Lucky – 2015-09-15T12:05:39.563

@Lucky though notice that the troubleshooting that I suggested doesn't involve knowing anything about what awk or gawk is doing – barlop – 2015-09-15T12:07:23.377

Answers

1

Try to replace the ' with " . So that the command looks like,

adb shell dumpsys package | awk -v RS="\n +Package" "/android\.permission\.CAMERA/{print $1}"

Take a look as well: Grep and Awk in Windows Invalid Char in Expression Error

duDE

Posted 2015-09-15T10:57:50.593

Reputation: 14 097

1Thanks. It was a simple double quote that didn't strike me for so long. ;) – Lucky – 2015-09-15T11:37:45.677

0

 It all depends on the version of GAWK you're running -- and how important it is to keep some old batch scripts working without modification.  (These days, if you're using AWK on Windows, it is almost certainly really GAWK.)  As duDE pointed out, changing the single quotes to double quotes will work -- but then you'll have to escape any double quotes inside the program.

 I just recently ran into this problem when I upgraded to GAWK 5.0.1 from v3.1.7, still running Windows 7.  (Yeah, I know...)  3.1.7 works just fine with the single quotes.  So, if you have some old scripts you want to keep without editing them or you just like the *established way* of running AWK, or just don't need whatever the newer versions bring, you might consider d/l'ing an older version.
 I don't know when it was changed, but someone dropped the ball in GAWK development somewhere between those two versions.  (I hate to say it, because I have been a pretty ardent M$ basher over the years and I have been a huge fan of public domain software and made a few contributions to the cause over the almost 30 years I've been programming and using other people's contributions.)  Truth is, we can't blame this one on Windows; it still works just fine with the old GAWK.  It's GAWK that introduced this problem.

Bob60506

Posted 2015-09-15T10:57:50.593

Reputation: 121