awkスクリプトを実行します。しかし、ここにあります。これはコマンドですawk -f awk_scr ERR.txt
。
BEGIN {
FS=" "
target="missing"
}
{
for (i=1; i <= NR; i++) {
for(j=1; j <= NF; j++) {
if ($j == target) {
do {
printf $j > "final.txt"
} while (j == NF)
}
if (j == NF) {
printf "\n"
}
}
}
}
内容はERR.txt
ここにあります。このawkスクリプトは、一致するコンテンツをトリミングするために使用されます。「失われた」その後、ファイルに印刷し、最終結果.txt。
npm ERR! peer dep missing: react@^15.0.0, required by [email protected]
npm ERR! peer dep missing: [email protected] - 3, required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
ただし、コマンドを実行すると空の標準出力のみが表示されます。
================内容を追加=================
ほんの数分しかかかりませんでしたが、それが私が期待していたものです。とにかく、awkスクリプトは今後も使い続ける価値があります。
missing: react@^15.0.0, required by [email protected]
missing: [email protected] - 3, required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
missing: [email protected], required by [email protected]
答え1
# awk_scr
BEGIN {
# clear the output file if it already exists
# you can do this using your shell instead
ORS = ""
print "" > "final.txt"
ORS = "\n"
}
/missing/ {
# delete all characters prior to the last occurrence of "missing:"
# this assumes that there is only one sub-string "missing:" per line
sub(/.*missing:/, "missing:", $0)
# append the updated line to the output file
print $0 >> "final.txt"
}
それからawk -f awk_scr ERR.txt && cat final.txt
。
sed
別の解決策は、より短いものを使用することです。
sed 's/^.*missing:/missing:/' ERR.txt | tee final.txt
しかし、awkソリューションと同じ欠点があります。つまり、「missing:」が 1 行に 1 回だけ発生すると仮定します。どちらも貪欲にawk
似合うからだsed
。
遅延マッチングをシミュレートするには、次のようにします(より良い)。
sed -E 's/(missing)(.*)|./\1\2/g' ERR.txt | tee final.txt
答え2
私はあなたのスクリプトが少し複雑で、「awk」の意味についての誤解を示していると思います。 awk プログラムは次のリストで構成されます。
pattern { actions }
氏名。新しいレコード/行を読み取るたびに、awkはパターン操作ステートメントを1つずつ実行して行を処理します。私が見ると、メモリ内のファイル全体をawkで読むことを想像しているようです。その後、レコード数と一緒にforループを使用してそれを処理できます。そのようなことは起こりませんでした。
次のawkがあなたの要求を行うことができます:
awk '/missing/' inputfile > "missing.txt"
ここでモードは、ジョブは/missing/
デフォルトジョブで、レコード全体を印刷するか1
同じです。{print $0}
このモード/missing/
状態は、「ミス」サブストリングを含むすべての行を選択します。