Count the number of warnings from the same report file in each sub-directory

Viewed 84

I am having one same file name**(report.txt)** in all sub-directories , for example I am showing the data for the file(report.txt) from 2 sub-directories as below:

report.txt in File1_D sub-directory:

  Stage           Family      Fatals    Errors  Warnings     Infos
  -----           --------  --------  --------  --------  --------
  CHECK           CODING           0         1         0         0
  CHECK           CONN             0         1         0         0
  LIST            CODING           0        12         0         0
  LIST            CONN             0        15         7         0
  -----           --------  --------  --------  --------  --------
  Total                            0        29         7         0

report.txt in File2_D sub-directory:

  Stage           Family      Fatals    Errors  Warnings     Infos  Remaining
  -----           --------  --------  --------  --------  --------  ----------
  CHECK           CODING           0         0         1         0           0
  LIST            CLK              0        49         0         0           0
  LIST            CODING           0         3         0         0           0
  LIST            CONN             0         2       598         0         196
  LIST            SYN              0         0       100         0         126
  -----           --------  --------  --------  --------  --------  ----------
  Total                            0        54       699         0         322

In which one file(report.txt) from one sub-directory will be having six columns and in other sub-directory file(report.txt) it will be of seven columns, I need the count of the Warnings column and the output like below :

File1_D:
Total number of warnings : 7

File2_D:
Total number of warnings : 699

Here File1_D and File2_D are the sub-directories names, I tried this code as below but I am not getting the desired output:

#!/bin/bash

    for d in *_D;
    do
    echo "$d:";
    if [[ "$NF" -eq 7 ]];
    then
        awk '/Total/{print "Total number of warnings:" $(NF-2);exit}' "$d"/report.txt
    else
        awk '/Total/{print "Total number of warnings:" $(NF-1);exit}' "$d"/report.txt
    fi
    done
1 Answers

Could you please try following, using find + awk combination here(also please give absolute path where I mentioned your_path).

find your_path -type f -iname "report.txt" -exec awk -f script.awk {} +

Where script.awk is as follows.

cat script.awk
#!/usr/bin/awk

FNR==1{
  print FILENAME":"
  for(i=1;i<=NF;i++){
    if(tolower($i)=="warnings"){  field=i  }
  }
}
/Total/{
  print "Total number of warnings:" $(field-1)
}


When I run this in my current directory /test/singh_test it gives my as follows results.

/test/singh_test/report.txt:
Total number of warnings:7
/test/singh_test/test1/report.txt:
Total number of warnings:699
Related