获取AWK结束前的总行数

时间:2013-03-11 20:47:39

标签: json awk git-log

我几乎要设法将整个git log输出为有效的JSON格式,现在唯一缺少的是在{{1}的末尾删除一个尾随的逗号} array (参见下面的输出)

由于我到目前为止使用JSON来实现这一点,我宁愿继续使用它,除非我可以使用其他东西获得完全相同的输出。我对任何建议持开放态度!

脚本放在awk存储库的根目录下,从终端运行,如:

git

(考虑到你已经cd到了存储库的根目录)

脚本:

./gitlog.sh

输出:

为了举例而缩短

git log --all --no-merges --shortstat --pretty=format:'commit_hash %H commit_hash_abbreviated %h tree_hash %T tree_hash_abbreviated %t parent_hashes %P parent_hashes_abbreviated %p author_email %ae author_date %ad author_date_unix_timestamp %at author_date_iso_8601 %ai committer_email %ce' | paste -d " " - - - | tail -r | awk -v q='"' -v c=':' -v cl='{' -v cr='}' -v bl='[' -v br=']' -v e=',' -v t='    ' '
BEGIN {
    print cl;
    print t q "commits" q c bl;
}
{
    count++;
    print t t cl;
    print t t t q "commit_nr" q c q count q e;
    print t t t q "commit_hash" q c q $2 q e;
    print t t t q "commit_hash_abbreviated" q c q $4 q e;
    print t t t q "tree_hash" q c q $6 q e;
    print t t t q "tree_hash_abbreviated" q c q $8 q e;
    if ( count == 1 ) {
        print t t t q "parent_hashes" q c q "" q e;
        print t t t q "parent_hashes_abbreviated" q c q "" q e;
        print t t t q "author_email" q c q $12 q e;
        print t t t q "date_day_week" q c q $14 q e;
        print t t t q "date_month_name" q c q $15 q e;
        print t t t q "date_month_day" q c q $16 q e;
        print t t t q "date_hour" q c q $17 q e;
        print t t t q "date_year" q c q $18 q e;
        print t t t q "date_hour_gmt" q c q $19 q e;
        print t t t q "author_date_unix_timestamp" q c q $21 q e;
        print t t t q "date_iso_8601" q c q $23 q e;
        print t t t q "committer_email" q c q $27 q e;
        print t t t q "files_changed" q c q $28 q e;
        print t t t q "insertions" q c q $31 q e;
        print t t t q "deletions" q c q $33 q e;
        print t t t q "impact" q c q $31 - $33 q;
    } else {
        print t t t q "parent_hashes" q c q $10 q e;
        print t t t q "parent_hashes_abbreviated" q c q $12 q e;
        print t t t q "author_email" q c q $14 q e;
        print t t t q "date_day_week" q c q $16 q e;
        print t t t q "date_month_name" q c q $17 q e;
        print t t t q "date_month_day" q c q $18 q e;
        print t t t q "date_hour" q c q $19 q e;
        print t t t q "date_year" q c q $20 q e;
        print t t t q "date_hour_gmt" q c q $21 q e;
        print t t t q "author_date_unix_timestamp" q c q $23 q e;
        print t t t q "date_iso_8601" q c q $25 q e;
        print t t t q "committer_email" q c q $29 q e;
        print t t t q "files_changed" q c q $30 q e;
        print t t t q "insertions" q c q $33 q e;
        print t t t q "deletions" q c q $35 q e;
        print t t t q "impact" q c q $33 - $35 q;
    }
    print t t cr e;
} END {
    print t br;
    print cr;
}' > gitlog.json

正如您所看到的,问题是 { "commits":[ { "commit_nr":"1", "commit_hash":"c7a397928f814f29028bccb281de60066395eaa1", "commit_hash_abbreviated":"c7a3979", "tree_hash":"e38dac0e625f63e877baa329204511ae490cd944", "tree_hash_abbreviated":"e38dac0", "parent_hashes":"", "parent_hashes_abbreviated":"", "author_email":"i@dreamyguy.com", "date_day_week":"Wed", "date_month_name":"Mar", "date_month_day":"6", "date_hour":"09:54:27", "date_year":"2013", "date_hour_gmt":"-0800", "author_date_unix_timestamp":"1362592467", "date_iso_8601":"2013-03-06", "committer_email":"i@dreamyguy.com", "files_changed":"1", "insertions":"4", "deletions":"", "impact":"4" }, { "commit_nr":"2", "commit_hash":"ee3810c9ff8fe144c9ee58f48d99f59885f03462", "commit_hash_abbreviated":"ee3810c", "tree_hash":"c6d102d3311a32a2475ba1539247f7ecc26b23d1", "tree_hash_abbreviated":"c6d102d", "parent_hashes":"c7a397928f814f29028bccb281de60066395eaa1", "parent_hashes_abbreviated":"c7a3979", "author_email":"i@dreamyguy.com", "date_day_week":"Thu", "date_month_name":"Mar", "date_month_day":"7", "date_hour":"20:20:42", "date_year":"2013", "date_hour_gmt":"+0100", "author_date_unix_timestamp":"1362684042", "date_iso_8601":"2013-03-07", "committer_email":"i@dreamyguy.com", "files_changed":"2", "insertions":"481133", "deletions":"", "impact":"481133" }, { "commit_nr":"3", "commit_hash":"bc9a179663f00f134041ac750a56df8280e0b50b", "commit_hash_abbreviated":"bc9a179", "tree_hash":"f80d83c9e8e14ff55b2d77921cb0479bd173bae9", "tree_hash_abbreviated":"f80d83c", "parent_hashes":"ee3810c9ff8fe144c9ee58f48d99f59885f03462", "parent_hashes_abbreviated":"ee3810c", "author_email":"i@dreamyguy.com", "date_day_week":"Thu", "date_month_name":"Mar", "date_month_day":"7", "date_hour":"20:22:03", "date_year":"2013", "date_hour_gmt":"+0100", "author_date_unix_timestamp":"1362684123", "date_iso_8601":"2013-03-07", "committer_email":"i@dreamyguy.com", "files_changed":"1", "insertions":"71", "deletions":"3", "impact":"68" }, ] } 行为循环中的每一行输出一个逗号(通过我命名为print t t cr e;的预定义awk变量),包括最后。 :(

我试过的尝试:

我将e替换为:

print t t cr e;

if (getline == 0) { print t t cr; } else { print t t cr e; } 应该在到达最后一行时返回getline,但它不是一个可靠的解决方案:它在我的25行输出的第13行(25次提交)中停止。我还使用官方git存储库(https://github.com/git/git/commits/master)测试了这种方法。在最后一次提交后我仍然得到尾随逗号。

由于脚本中存在一些缺陷,因此0可能无法获得真正的行数。但getlineNR都会返回准确的行数。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

您可以尝试的一种解决方案是在下一行的开头添加逗号,而不是在上一行的末尾添加逗号,并使用printf函数来处理换行符。

awk脚本中,我检查行是否不是第一行并添加逗号:

BEGIN {
    print cl;
    print t q "commits" q c bl;
}
{
    count++;
    #--------------------
    if ( count > 1 ) {
      printf "%s\n", e
    }
    #--------------------
    ...
} END {
    ...
}' > gitlog.json

在最后一行的印刷品中,我使用printf来避免或添加额外的换行符:

...
printf "%s%s%s", t, t, cr;
} END {
    printf "\n%s%s\n", t, br;
    print cr;
}' > gitlog.json

我测试了它,似乎有用。我希望它对你有用。