Sample Header Ad - 728x90

sed (or awk): print captured group or placeholder if it doesn't exist

3 votes
7 answers
546 views
So since GitHub removed the insights tab for non-premium accounts, I'm trying to locally list insertions and deletions to my git repository per day. I figured out this way of printing what I want:
git log --pretty="@%ad" --date=short --shortstat |  tr "\n" " " | tr "@" "\n"
That produces this kind of output:
2024-06-13   7 files changed, 400 insertions(+), 406 deletions(-) 
2024-06-12   3 files changed, 145 insertions(+) 
2024-06-12   5 files changed, 638 deletions(-) 
2024-06-12   1 file changed, 1 insertion(+), 1 deletion(-)
Notice the plurals in file(s), insertion(s) and deletion(s). Another problem is that a commit might not have insertions or deletions (or both, but let's ignore this case). So I'm almost there, I just need to extract the date, insertions and deletions and group by date. That will produce some sort of "work done per day" graph. I made this regex to capture the fields dealing with all the optionals:
-regex
/^([0-9]{4}-[0-9]{2}-[0-9]{2})\s{3}[0-9]+\sfile(s)?\schanged,\s(([0-9]+)\sinsertion(s)?\(\+\))?(,\s)?(([0-9]+)\sdeletion(s)?\(\-\))?\s$/gm
Now I need to get the 1st, 4th and 8th groups, for instance with sed:
echo "2024-06-13   7 files changed, 400 insertions(+), 406 deletions(-) " |
    sed -r 's/^([0-9]{4}-[0-9]{2}-[0-9]{2})\s{3}[0-9]+\sfile(s)?\schanged,\s(([0-9]+)\sinsertion(s)?\(\+\))?(,\s)?(([0-9]+)\sdeletion(s)?\(\-\))?\s$/\1 \4 \8/gm'
That produces the correct output:
2024-06-13 400 406
But if the input string doesn't have insertions or deletions, sed just prints nothing for that captured group. Eg.:
2024-06-13 400
And I have no way to tell if the single number are insertions or deletions. **Is there anyway to extract the groups from each line, but print a "0" as placeholder if the group doesn't exist?** (not necessarily with sed alone, and not necessarily in a single command).
Asked by Mister Smith (133 rep)
Aug 10, 2024, 10:26 PM
Last activity: Aug 12, 2024, 08:01 PM