I need to extract numbers from several files into a cumulative variable and print that variable for each parent directory as shown
├───Parent1
│ ├───20210824_2000
│ │ ├───200000_child1
│ │ │ report.md
│ │ │ log.log
│ │ │ file.xml
│ │ │ input.json
│ │ │
│ │ ├───200030_child2
│ │ │
│ │ ├───200034_child3
│ │ │
│ │ ├───200039_child4
│ │ ...
│ ├───20210825_0800
│ │ ├───200000_child1
│ │ │ report.md
│ │ │ log.log
│ │ │ file.xml
│ │ │ input.json
│ │ │
│ │ ├───200030_child2
│ │ │
│ │ ├───200034_child3
│ │ │
│ │ ├───200039_child4
│ │ ...
│ ...
├───Parent2
│ ├───20210824_2000
│ │ ├───200000_child1
│ │ │ report.md
│ │ │ log.log
│ │ │ file.xml
│ │ │ input.json
│ │ │
│ │ ├───200030_child2
│ │ │
│ │ ├───200034_child3
│ │ │
│ │ ├───200039_child4
│ │ ...
│ ├───20210825_0800
│ │ ├───200000_child1
│ │ │ report.md
│ │ │ log.log
│ │ │ file.xml
│ │ │ input.json
│ │ │
│ │ ├───200030_child2
│ │ │
│ │ ├───200034_child3
│ │ │
│ │ ├───200039_child4
│ │ ...
│ ...
...
I can't seem to extract the grep output into a numeric variable. The child folder has a timestamp attached so I sort since I only want the latest files.
Here's what I have so far:
#!/bin/bash
find . -type d -iname 'parent*' | while read -r dir; do
sum=0;
find "$dir" -maxdepth 1 -type d | sort -r | head -1 |
(
while read -r subdir; do
count="$(find "$subdir" -type f -iname '*report.md' -exec grep -ohP '(?<=\*)\d+(?=\*+ number of things)' {} \+)"
sum=$((sum + count))
done
basename "$dir" "$sum"
)
done
But this doesn't seem to want to add count
to sum
it rather just prints count
to the console for each file.