Awk 格式化输出并添加日期时间戳
Awk formatting of the output and adding a date time stamp
场景:
- 我有两个文件。 File1(制表符分隔),File2(字符串)。在 File1 中,我
组合01行的Field4+Field3+Field2来做一个
File2 中字符串的 Field1 的引用键。
- 我能匹配和提取信息,但格式不对
要求
在输出 # 1 中,我需要来自 file1.txt 的 7 条不匹配行,然后是第 99 行。我需要将相同的行复制到输出 #2 但没有 99。如果您需要更多详细信息,请告诉我
Awk Script(我正在使用)
awk 'FNR == NR && ! /^[[:space:]]*$/ { key = substr(, 1, 8); a[key] = [=10=]; next }
== "01" { if (code != 0)
{
if (code in a)
{
printf("77\t%s\n", a[code])
delete a[code]
}
}
code =
}
{ 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(输入)
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(输入)
50006889 CCARD /3010 /E /C A87545457 / // ///11 ///
51002387 CCARD /3000 /E /S N054896334IV / // ///11 ///
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
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
gawk '
BEGIN {
OFS="\t"
date = strftime("%Y-%m-%d", systime())
out = "File3.txt"
err = "File4.txt"
}
NR==FNR && NF {line[]=[=10=]; next}
function print_77_99() {
if (key in line)
print "77", line[key] > out
else {
print "99", date > out
printf "%s", lines >> err
}
}
== "01" {
if (FNR > 1) print_77_99()
key =
lines = ""
}
{
print > out
lines = lines [=10=] "\n"
}
END {print_77_99()}
' File2.txt File1.txt
场景:
- 我有两个文件。 File1(制表符分隔),File2(字符串)。在 File1 中,我 组合01行的Field4+Field3+Field2来做一个 File2 中字符串的 Field1 的引用键。
- 我能匹配和提取信息,但格式不对
要求 在输出 # 1 中,我需要来自 file1.txt 的 7 条不匹配行,然后是第 99 行。我需要将相同的行复制到输出 #2 但没有 99。如果您需要更多详细信息,请告诉我
Awk Script(我正在使用)
awk 'FNR == NR && ! /^[[:space:]]*$/ { key = substr(, 1, 8); a[key] = [=10=]; next }
== "01" { if (code != 0)
{
if (code in a)
{
printf("77\t%s\n", a[code])
delete a[code]
}
}
code =
}
{ 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(输入)
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(输入)
50006889 CCARD /3010 /E /C A87545457 / // ///11 ///
51002387 CCARD /3000 /E /S N054896334IV / // ///11 ///
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
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
gawk '
BEGIN {
OFS="\t"
date = strftime("%Y-%m-%d", systime())
out = "File3.txt"
err = "File4.txt"
}
NR==FNR && NF {line[]=[=10=]; next}
function print_77_99() {
if (key in line)
print "77", line[key] > out
else {
print "99", date > out
printf "%s", lines >> err
}
}
== "01" {
if (FNR > 1) print_77_99()
key =
lines = ""
}
{
print > out
lines = lines [=10=] "\n"
}
END {print_77_99()}
' File2.txt File1.txt