如何在 Windows 7 中运行 awk 命令?

如何在 Windows 7 中运行 awk 命令?

我有以下adb+awk命令,可以在 Linux 环境中正常工作[参考]

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

但我需要在 Windows 7 PC 中运行此命令GnuWin32 的 Gawk软件包已安装。我可以从C:\Program Files (x86)\GnuWin32\bin文件夹运行 awk 命令,并从文件夹运行 adb 命令C:\Program Files (x86)\Android\android-sdk\platform-tools。我需要运行上述命令并获取允许 CAMERA 权限的软件包列表。

为 AWKPATH 和 PATH 变量设置 Windows 环境变量不起作用。所以我只是复制了 GnuWin32 gawkbin文件夹的内容并粘贴到该platform-tools文件夹​​中。但是当我在命令提示符中运行时,我得到了

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

如何在 Windows 中运行上述命令?或者我可以运行的正确表达式是什么?

答案1

尝试将 替换'"。这样命令看起来就像,

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

也看一下:Windows 中的 Grep 和 Awk 表达式错误中的无效字符

答案2

 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.

相关内容