我试图从多个输入中输出,并且输出将根据输入变量分为不同的名称。
从此输入
==> input_A.txt <==
model
61096525 3380677 9639
61096526 3382521 9634
61096527 3384122 9651
61096528 3384250 9675
61096529 3384313 9706
==> input_B.txt <==
model
10911991 396855811255
10911992 396928611344
10911993 396943311341
10911994 397088711443
10911995 397208211507
将被导出到单独的文件input_B.txt
= schema
和input_A.txt
= lst
==> output / input_A.lst <==
Column 1 Column 2 Column 3
A 6109.00 6525.00
A 6109.00 6526.00
A 6109.00 6527.00
A 6109.00 6528.00
A 6109.00 6529.00
==> output / input_B.schema <==
Column 1 Column 2 Column 3
B 1091.00 1991.00
B 1091.00 1992.00
B 1091.00 1993.00
B 1091.00 1994.00
B 1091.00 1995.00
#!/bin/awk -f
FNR==1{
sub(/.txt/,"",FILENAME)
lst="output/"FILENAME".lst"
schema="output/"FILENAME".schema"
print "Column 1 Column 2 Column 3 " > (lst)
print "Column 1 Column 2 Column 3 " > (schema)
}
/^ [6-9]/{
Name=substr($0,18,4)
No=substr($0,22,4)
printf ("A%10.2f%10.2f\n",Name,No) > (lst)
}
/^ [1-4]/{
Name=substr($0,18,4)
No=substr($0,22,4)
printf ("B%10.2f%10.2f\n",Name,No)> (schema)
}
几乎成功,我需要您的建议。我需要从输出中跳过那些不匹配的字段。
script.awk *txt
head output/*
==> output/input_A.lst <==
Column 1 Column 2 Column 3
A 6109.00 6525.00
A 6109.00 6526.00
A 6109.00 6527.00
A 6109.00 6528.00
A 6109.00 6529.00
==> output/input_A.schema <== This one should not being output
Column 1 Column 2 Column 3
==> output/input_B.lst <== also this one should not being output, due I'm printing it at the FNR
Column 1 Column 2 Column 3
==> output/input_B.schema <==
Column 1 Column 2 Column 3
B 1091.00 1991.00
B 1091.00 1992.00
B 1091.00 1993.00
B 1091.00 1994.00
B 1091.00 1995.00
答案 0 :(得分:1)
能否请您尝试以下。这应该确保至少有1行不仅进入标题而进入输出文件。如果文件不匹配,则此文件将不会创建仅包含标题的空文件。
#!/bin/awk -f
FNR==1{
sub(/.txt/,"",FILENAME)
lst="output/"FILENAME".lst"
schema="output/"FILENAME".schema"
count=count1=""
}
/^ [6-9]/{
if(++count==1){
print "Column 1 Column 2 Column 3 " > (lst)
}
Name=substr($0,18,4)
No=substr($0,22,4)
printf ("A%10.2f%10.2f\n",Name,No) > (lst)
}
/^ [1-4]/{
if(++count1==1){
print "Column 1 Column 2 Column 3 " > (schema)
}
Name=substr($0,18,4)
No=substr($0,22,4)
printf ("B%10.2f%10.2f\n",Name,No)> (schema)
}