I'm learning awk. In trying to find a solution for a question, I came with the next code, but I'm stuck at the end¹. Explanation:
Having file with contents:
H|20200425|abcd| # header
R|abc|2019-03-06|100.00
R|abc|2019-03-06|15.00
R|abc|2019-03-06|10.00
Add at the end
T|20200425|-count of records-|-sum of 4th column-Direct the output to a file
20190306.txt, the date of 3rd column, in the format YYYYMMDD.
My attempt:
awk -F'|' '
# get the date formatted
NR == 2 {
d = $3; gsub(/-/,"",d)
}
# get the 2nd field of the header
NR == 1 {
a = $2
}
# if the line starts with 'R', sum the column and get the count of them
$1 == "R" {
sum += $4
++c
}
# print the final line with variables acquired
END {
OFS = "|"; print "T",a,c,sum".00"
}1
' file
This command gives me the "desired" result:
H|20200425|abcd|
R|abc|2019-03-06|100.00
R|abc|2019-03-06|15.00
R|abc|2019-03-06|10.00
T|20200425|3|125.00
The d variable is 20190306.
But the problem, and what I'm asking, is how to redirect this output to the file 20190306.txt.
¹Sure it is bad coding of course, (sigh, brain hurts), but my goal is to keep the question as focused as possible, I'm not asking to point all the mistakes.