我运行 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:”。这是因为两者awk
都sed
贪婪地匹配。
要模拟惰性匹配,您可以执行以下操作(更好):
sed -E 's/(missing)(.*)|./\1\2/g' ERR.txt | tee final.txt
答案2
我相信您的脚本有点复杂,并且显示出对“awk”含义的误解。 awk 程序由以下列表组成
pattern { actions }
陈述。每次读取新记录/行时,awk 都会通过逐一执行模式操作语句来处理该行。从我看来,您似乎想象 awk 读取内存中的完整文件,然后您可以使用 for 循环处理记录数来处理它。事情不是这样的。
以下 awk 可能会执行您的请求:
awk '/missing/' inputfile > "missing.txt"
这里,模式是/missing/
,操作是默认操作1
,等于{print $0}
,或打印完整记录。该模式/missing/
状态,选择包含子字符串“missing”的所有行