Windowsのgrepとawk式エラーの無効なchar
質問
私はGrepとAwkを初めて使用しています - Windows 7を使用しています(GnuwinからWindows用にGrepとAWKをダウンロードしました)。
私はこのスクリプトの実行に問題があります:
grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}'
エラーが発生します:
awk: '{print
awk: ^無効なchar '' '' '' '
Windowsで二重引用符を使用しなければならないことと関係があると思いますが、考えられるすべての組み合わせを試してみましたが、それでもうまくいきません。
誰かが助けることができますか?ありがとう
解決
コマンドラインアイテムの脱出は、常に窓の痛みです。最後の手段として、おそらく使用できます gawk -f
!
だから:あなたのファイル script.awk
含まれる:
print $2,$1
あなたがやる grep -Fwf dictionary.txt frequency.txt | awk -f script.awk
他のヒント
Windowsでは、awkコマンドを引用するために二重引用符を使用する必要があります。そう
grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}'
に変更する必要があります
grep -Fwf dictionary.txt frequency.txt | awk "{print $2 "," $1}"
しかし、覚えておいてください、二重引用符内で二重引用符を使用することはできません。それらを逃れる必要があります。 Windowsでは、単に使用することはできません \ それを逃れるために。次の構文が必要です。
grep -Fwf dictionary.txt frequency.txt | awk "{print $2 \"",\"" $1}"
そうです \"" 表現します " 内部二重引用符。
awkスクリプトの周りに二重引用符を使用し、古き良きバックスラッシュを使用して、印刷ステートメントの埋め込み引用符を逃がす必要があります:[g] awk "begin {print " hello Escape char! "}"
input.csvを受け入れ、出力を受け入れる短い例を次に示します new.csv
:
gawk < input.csv -F, "{print $1 \"",\"" $5} ">new.csv
ブラケットは二重引用符内にある必要があるため、ブラケット式の内側には3セットの二重引用符が必要です。
例えば:
gawk "{print $2 """,""" $1}"
私は長年にわたってWindowsの下でawkを走らせるためにugbめてきました。引用符とパスデリミターに問題があります。私の最後の解決策は、コマンドラインがない「Awk Fly Free」を自由にすることです。 UnixスタイルのコマンドラインJujuの接着剤として開発されたことを理解していますが、スクリプト言語として使用したかっただけです。
すべてのawkスクリプトには、ターゲットのリストと定義された出力ファイルが含まれています。関連するDOSバッチファイルをダブルクリックすることで実行できます。
: 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
私のawkスクリプトの例を以下に示します(:: tabですべての行を抽出し、それらを印刷します):
# 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 ""
}
}
これは、スクリプト内の入力ターゲットを定義し、スクリプト内の最終出力ターゲットを定義することがわかります。 Temp ".out"ファイルを使用して、多くの印刷リダイレクトを回避し、スクリプトの最後のセクションでファイルを目的の出力にコピーします。
このバッチファイルにawkファイルを関連付けており、エディターにオプションを追加して、awkファイルをバッチファイルに送信しました。
敬具
Yousuiは言った:
...二重引用符内で二重引用符を使用することはできません。それらを逃れる必要があります。 Windowsでは、単にを使用して逃げることはできません。
Loccantbiosguyは彼の例では "を使用しています。
""と "の両方を試しましたが、どちらも私のために機能します(コマンドラインとバッチファイルの両方でWindows XPの下でGAWK)。
「in the output(c code 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