我希望从 file2 打印整个字符串.txt但它只打印 77 而不是从 File2 打印整个匹配的字符串.txt 任何帮助不胜感激。谢谢
脚本
awk '
BEGIN {
OFS="t"
out = "File3.txt"}
NR==FNR && NF {a[substr($0,1,8)]=$0; next}
function print_77_99() {
if (key in a)
print "77", a[key] > out
}
$1 == "01" {
if (FNR > 1) print_77_99()
key = $4 $3 $2
lines = ""
}
{ print > out
lines = lines $0 "n" } END {print_77_99()}
' File2.txt File1.txt
文件1.txt
01 89 68 5000
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT0429
01 44 73 8800
02 44 73
04 44 73 02
03 44 73
06 44 73
07 44 11 RT 0789
文件2.txt
50006889RT0429 NARD /3010 /E /C A87545457 / // ///11 ///
51002387 NARD /3000 /E /S N054896334IV / // ///11 ///
文件3.txt(电流输出)
01 89 68 5000
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT0429
77
01 44 73 8800
02 44 73
04 44 73 02
03 44 73
06 44 73
07 44 11 RT 0789
期望的输出
01 89 68 5000
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT0429
77 50006889RT0429 NARD /3010 /E /C A87545457 / // ///11 ///
01 44 73 8800
02 44 73
04 44 73 02
03 44 73
06 44 73
07 44 11 RT 0789
我认为错误出在函数中:
function print_77_99() {
if (key in a)
print "77", line[key] > out
}
您将 File2.txt 内容保存在数组a
但稍后尝试从数组line
读取它。请改用a
,例如:
function print_77_99() {
if (key in a)
print "77", a[key] > out
}