Reputation: 193
I tried to output from multiple inputs, and the output will be split into separated names according to the input variable.
From this input
==> 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
Will be exported into separated file input_B.txt
= schema
and 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
So I 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)
}
Almost succeeded, I need your advise. I need to skip those unmatched field from being output like this.
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
Upvotes: 0
Views: 48
Reputation: 133650
Could you please try following. This should make sure that at least 1 line is coming into Output file(s) not only header. If a file doesn't have any match then an empty file with only headers will NOT be created by this.
#!/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)
}
Upvotes: 1