情境:
要求 在输出#1中,我需要来自file1.txt的7个No Matched行,然后是99行。我需要将相同的行复制到Output#2但没有99.如果您需要更多详细信息,请告诉我。
Awk脚本(我正在使用)
awk 'FNR == NR && ! /^[[:space:]]*$/ { key = substr($1, 1, 8); a[key] = $0; next }
$1 == "01" { if (code != 0)
{
if (code in a)
{
printf("77\t%s\n", a[code])
delete a[code]
}
}
code = $4$3$2
}
{ print }
END {
if (code in a)
{
printf("77\t%s\n", a[code])
delete a[code]
}
for (code in a)
printf("99\t%s\n", a[code])
}' \
File2.txt File1.txt > File3.txt
awk -F '\t', '/^99/' File3.txt > File4.txt
FILE1.TXT(INPUT)
01 89 68 5000
02 89 11
03 89 00
06 89 00
07 89 19 RT 0428
01 87 23 5100
02 87 11
04 87 9 02
03 87 00
06 87 00
07 87 11 RT 0428
01 83 23 4900
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT 0428
File2.txt(INPUT)
50006889 CCARD /3010 /E /C A87545457 / // ///11 ///
51002387 CCARD /3000 /E /S N054896334IV / // ///11 ///
51002390800666 CCARD /3000 /E /S N0978898IV / // ///11 ///
File3.txt(OUTPUT#1)
01 89 68 5000
02 89 11
03 89 00
06 89 00
07 89 19 RT 0428
77 50006889 CCARD /3010 /E /C A87545457 / // ///11 ///
01 87 23 5100
02 87 11
04 87 9 02
03 87 00
06 87 00
07 87 11 RT 0428
77 51002387 CCARD /3000 /E /S N054896334IV / // ///11 ///
01 83 23 4900
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT 0428
99
99 51002390800666 CCARD /3000 /E /S N0978898IV / // ///11 ///
File4.txt(输出#2)
99
99 51002390800666 CCARD /3000 /E /S N0978898IV / // ///11 ///
File3.txt(期望的输出#1)
01 89 68 5000
02 89 11
03 89 00
06 89 00
07 89 19 RT 0428
77 50006889 CCARD /3010 /E /C A87545457 / // ///11 ///
01 87 23 5100
02 87 11
04 87 9 02
03 87 00
06 87 00
07 87 11 RT 0428
77 51002387 CCARD /3000 /E /S N054896334IV / // ///11 ///
01 83 23 4900
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT 0428
99
01 44 73 8800
02 44 73
04 44 73 02
03 44 73
06 44 73
07 44 11 RT 0789
99
(When NO MATCH, THERE IS ONLY one line 99 <tab> <date> in the end of 7 lines and then the next 7 lines in case of another no match and then 99 <tab> <date> and so on)
File4.txt(期望的输出#2)
01 83 23 4900
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT 0428
(当前输入文件只有一个不匹配,我想继续添加其他不匹配的行,没有99后缀到这个文件,所以它会有如下结构)
01 83 23 4900
02 83 11
04 83 9 02
03 83 00
06 83 00
07 83 11 RT 0428
01 38 66 7000
02 38 66
04 38 66 02
03 38 66
06 38 66
07 38 66 RT 0428
01 44 73 8800
02 44 73
04 44 73 02
03 44 73
06 44 73
07 44 11 RT 0789
答案 0 :(得分:0)
gawk '
BEGIN {
OFS="\t"
date = strftime("%Y-%m-%d", systime())
out = "File3.txt"
err = "File4.txt"
}
NR==FNR && NF {line[$1]=$0; next}
function print_77_99() {
if (key in line)
print "77", line[key] > out
else {
print "99", date > out
printf "%s", lines >> err
}
}
$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