
Paul F. answered 06/24/20
IT and Math Tutor
The best way to tackle the full problem is to a short script. Here are the elements of it.
The main way to pull line numbers using grep is the "-n" option.
But this is not in the desired format. However there are ways to process each line returned by the command using the while/read construct. Here is the full script:
Noticed that the actual "grep -c" command is redirected in using the $() nomenclature and a triple input redirect symbol.
This script will work no matter how many digits are in the line number count. It will also work regardless of other colons showing up in the input file.
You can also see how the last line is formatted.The "echo -e" command is used to insert a leading newline with the "\n" symbol. Here's the output: