Search code examples
bashshell

Replace a record with a value zero when a specific number is not find with shellscript


I have a file as shown below, I'm taking the last number on the column $2 and counting how many records are there finishing from 0 to 9, but sometimes there is no record with 0 to 9 so I need to replace it with result zero. I mean when I don't have any record on $2 finishing with for example with number 2, I will put as a result number 2 --> 0

Input file example:

2022-11-17 05:00:02.327, MSG:86442, AppID:22
2022-11-17 05:00:10.829, MSG:81874, AppID:9
2022-11-17 05:00:14.143, MSG:81778, AppID:10
2022-11-17 05:00:16.365, MSG:81782, AppID:22
2022-11-17 05:00:25.010, MSG:82959, AppID:22
2022-11-17 05:00:30.647, MSG:58653, AppID:22
2022-11-17 05:00:40.852, MSG:58198, AppID:11
2022-11-17 05:00:45.104, MSG:89039, AppID:22
2022-11-17 05:00:45.221, MSG:83564, AppID:21
2022-11-17 05:01:00.618, MSG:34115, AppID:20
2022-11-17 05:01:02.692, MSG:86963, AppID:21
2022-11-17 05:01:02.927, MSG:81387, AppID:10
2022-11-17 05:01:04.826, MSG:82119, AppID:11
2022-11-17 05:01:04.926, MSG:82111, AppID:11
2022-11-17 05:01:04.945, MSG:82116, AppID:13
2022-11-17 05:01:00.618, MSG:59110, AppID:20

Output:

# awk -F ',' '{print $2}' test.log|cut -c 10-10|sort|uniq -c
      1 0
      1 1
      2 2
      2 3
      2 4
      1 5
      1 6
      1 7
      2 8
      3 9

I try as shown below, but I need to put zero to field where I don't have any records found in $2 at the end with 0 to 9:

2022-11-17 05:00:02.327, MSG:86442, AppID:22
2022-11-17 05:00:14.143, MSG:81778, AppID:10
2022-11-17 05:00:16.365, MSG:81782, AppID:22
2022-11-17 05:00:25.010, MSG:82959, AppID:22
2022-11-17 05:00:40.852, MSG:58198, AppID:11
2022-11-17 05:00:45.104, MSG:89039, AppID:22
2022-11-17 05:01:00.618, MSG:34115, AppID:20
2022-11-17 05:01:02.927, MSG:81387, AppID:10
2022-11-17 05:01:04.826, MSG:82119, AppID:11
2022-11-17 05:01:04.926, MSG:82111, AppID:11
2022-11-17 05:01:04.945, MSG:82116, AppID:13
2022-11-17 05:01:00.618, MSG:59110, AppID:20

Output expectation :

awk -F ',' '{print $2}' test.log|cut -c 10-10|sort|uniq -c

nuberofrecords N° from0 to 9

# awk -F ',' '{print $2}' test.log|cut -c 10-10|sort|uniq -c
nuberofrecords N°from0 to 9
      1 0
      1 1
      2 2
      **0 3**
      **0 4**
      1 5
      1 6
      1 7
      2 8
      3 9

Solution

  • You may try this awk command:

    awk -F, '
            { freq[substr($2,length($2))]++ }
        END { for (i=0; i<=9; ++i) print freq[i]+0, i }
    ' file