Artificial intelligent assistant

Count lines ending in "*" I have several files in a directory with this kind of content: Wood * Nails Large Hammer * Some names have a star after them, some don't. I have multiple files with such content. In each file a product may or may not have a **single** star next to it. I need to make a bash script to count the number of star occurrences for each individual product in all the files. For example, the output needs to be like this: Wood 12 Yellow Lamps 6 Nails 4 ... Which means that in all the files it found 12x a star next to Wood, 6x a star next to the lamps, etc... It's pretty easy to parse it in C, but I don't want a binary to run. I want a shell script, and I'm not as versatile with grep and awk, which I'm sure I need here. I know how to count the stars per se, but I'm not sure how to track which star count belongs to which product.

Like this, with one awk:


awk '$NF=="*"{$NF=""; arr[$0]++}END{for (i in arr) print i arr[i]}' ./*


* `$NF` is the latest string separated by space(s) by default
* the main trick is to create an associative named `arr`ay with the current words as _key_ and incrementing as _value_
* at the `END` we iterate over the `arr`ay to `print` each keys/values



With perl one-liner:


perl -anE '
if ($F[-1] eq "*") {
$k = join " ", @F[0..@F-2];
$a->{$k}++
}
END{say "$_ $a->{$_}" for keys %$a}
' ./*


The `-a` is the _split_ mode in `@F` default array

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 1d635565a7afb57fe451e7e37fde0ada