如何将 awk 的输出重定向到文件?

How to redirect output of awk to a file?

我有这个 awk 命令可以用在 replacement.txt、

中找到的字符串替换 file.txt 中出现的 "proto"
awk 'FNR==NR{a[++i]=[=10=]; next} /proto/{sub(/proto/, a[++j])} 1' replacement.txt file.txt

完美运行,但未编辑原始文件。我用 >> 将它重定向到文件,但输出没有重定向

awk 'FNR==NR{a[++i]=[=11=]; next} /proto/{sub(/proto/, a[++j])} 1' replacement.txt file.txt >> "file.txt"

有人可以给我提示吗?

gawk > 4.1:

gawk -i inplace 'FNR==NR{a[++i]=[=10=]; next} /proto/{sub(/proto/, a[++j])} 1' replacement.txt file.txt

如果您没有此功能,请使用临时文件名,然后mv temp file.txt

她就是你如何使用 awk

awk 'some program' file > tmp && mv tmp file

对于您的数据:

awk 'FNR==NR{a[++i]=[=11=]; next} /proto/{sub(/proto/, a[++j])} 1' replacement.txt file.txt > tmp && mv tmp file.txt