我尝试从多个输入输出,输出将根据输入变量被分成不同的名称。
从下面的输入
==> 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中
输入输出/ ==> _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==>输出/输入_B.模式<==
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所以我followed the script了。
#!/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发布于 2019-10-09 15:26:27
你能不能试着跟上。这应该确保输出文件中至少有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)
}https://stackoverflow.com/questions/58298357
复制相似问题