如何在 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 gawk資料夾的內容bin並貼上到該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.

相關內容