in Education by
I have a report for year-month entries like below 201703 5 201708 10 201709 20 201710 40 201711 80 201712 100 201802 0 201803 25 201804 50 201805 50 201806 150 201807 300 201808 200 201902 10 I need to sum the year-month entries by year and print after all the months for that particular year. The year-month can have missing entries for any month(s). For those months the a dummy value (0) should be inserted. Required output: 201703 5 201704 0 201705 0 201706 0 201707 0 201708 10 201709 20 201710 40 201711 80 201712 100 2017 255 201801 0 201802 0 201803 25 201804 50 201805 50 201806 150 201807 300 201808 200 201809 0 201810 0 201811 0 201812 0 2018 775 201901 0 201902 10 201903 0 2019 10 I can get the summary of year by using below command. awk ' { c=substr($1,0,4); if(c!=p) { print p,s ;s=0} s=s+$2 ; p=c ; print } ' ym.dat But, how to insert entries for the missing ones?. Also the last entry should not exceed current (system time) year-month. i.e for this specific example, dummy values should not be inserted for 201904..201905.. etc. It should just stop with 201903 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You may use this awk script mmyy.awk: { rec[$1] = $2; yy=substr($1, 1, 4) mm=substr($1, 5, 2) + 0 ys[yy] += $2 } NR == 1 { fm = mm fy = yy } END { for (y=fy; y<=cy; y++) for (m=1; m<=12; m++) { # print previous years sums if (m == 1 && y-1 in ys) print y-1, ys[y-1] if (y == fy && m < fm) continue; else if (y == cy && m > cm) break; # print year month with values or 0 if entry is missing k = sprintf("%d%02d", y, m) printf "%d%02d %d\n", y, m, (k in rec ? rec[k] : 0) } print y-1, ys[y-1] } Then call it as: awk -v cy=$(date '+%Y') -v cm=$(date '+%m') -f mmyy.awk file 201703 5 201704 0 201705 0 201706 0 201707 0 201708 10 201709 20 201710 40 201711 80 201712 100 2017 255 201801 0 201802 0 201803 25 201804 50 201805 50 201806 150 201807 300 201808 200 201809 0 201810 0 201811 0 201812 0 2018 775 201901 0 201902 10 201903 0 2019 10

Related questions

0 votes
    I have a report for year-month entries like below 201703 5 201708 10 201709 20 201710 40 201711 80 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a report for year-month entries like below 201703 5 201708 10 201709 20 201710 40 201711 80 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I have a parameters.ini file, such as: [parameters.ini] database_user = user database_version = 20110611142248 I want to ... I do this? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I want to list the public IP addresses of my EC2 instances using Bash, separated by a delimiter (space or ... something else entirely? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I'm starting a bash script which will take a path in S3 (as specified to the ls command) and dump the ... finding which does it? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Let's say there is a table structured like this: ID | article_id | article_count | created_at ---| ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    Let's say there is a table structured like this: ID | article_id | article_count | created_at ---| ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm following @zdim's recursive solution in this question fastest way to sum the file sizes by owner in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Six faces of a die are marked with numbers 1,-1,0,-2,2,3 and the die is thrown thrice. The probability that the ... C. 5 108 D. 1 36 Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
0 votes
    Which of the following input control represents a date consisting of a year and a week number encoded according to ISO 8601 in Web Form 2.0? A - week B - time C - number D - range...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    Which of the following input control represents a date consisting of a year and a month encoded according to ISO 8601 in Web ... - datetime B - datetime-local C - date D - month...
asked Dec 1, 2020 in Technology by JackTerrance
0 votes
    class year { public static void main() { int a=2000; if( a%4 == 0 && a%100 != 0); { System.out.println ... this it is showing 2 errors Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
0 votes
    Which of the following input control represents a date (year, month, day) encoded according to ISO 8601 in Web Form 2.0?...
asked Apr 10, 2021 in Education by JackTerrance
...