【发布时间】:2023-04-21 23:14:01
【问题描述】:
gcov 是一个 GNU 工具链实用程序,可生成代码覆盖率报告(参见 documentation),格式如下:
-: 0:Source:../../../edg/attribute.c
-: 0:Graph:tmp.gcno
-: 0:Data:tmp.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
-: 3:int main (void)
1: 4:{
1: 5: int i, total;
-: 6:
1: 7: total = 0;
-: 8:
11: 9: for (i = 0; i < 10; i++)
10: 10: total += i;
-: 11:
1: 12: if (total != 45)
#####: 13: printf ("Failure\n");
-: 14: else
1: 15: printf ("Success\n");
1: 16: return 0;
-: 17:}
我需要提取从 bash 脚本中执行的行的行号。 $ egrep --regexp='^\s+[1-9]' example_file.c.gcov 似乎返回了相关行。典型输出的一个例子是:
1: 978: attr_name_map = alloc_hash_table(NO_MEMORY_REGION_NUMBER,
79: 982: for (k = 0; k<KNOWN_ATTR_TABLE_LENGTH; ++k) {
78: 989: attr_name_map_entries[k].descr = &known_attr_table[k];
78: 990: *ep = &attr_name_map_entries[k];
1: 992:} /* init_attr_name_map */
519: 2085: new_attr_seen = FALSE;
519: 2103: p_attributes = last_attribute_link(p_attributes);
519: 2104: } while (new_attr_seen);
519: 2106: return attributes;
16: 3026:void transform_type_with_gnu_attributes(a_type_ptr *p_type,
16: 3041: for (ap = attributes; ap != NULL; ap = ap->next) {
1: 6979:void process_alias_fixup_list(void)
1: 6984: an_alias_fixup_ptr entries = alias_fixup_list, entry;
我随后必须提取行号字符串。此示例的预期输出为:
978
982
989
990
992
2085
2103
2104
2106
3026
3041
6979
6984
有人可以提出一种可靠、稳健的方法来实现这一目标吗?
注意:
我的想法是消除字符: 的第一个和第二个实例之间没有放置的所有内容,我尝试使用sed 进行此操作,但到目前为止没有取得多大成功。
【问题讨论】:
-
如果您对特定于 bash 的代码感到满意,参数扩展将比 sed/awk/etc 其他外部进程/程序更快地完成此操作。
example="123: 456: 789abc#\$&-+)({}\/"; example="${example#*\:}"; echo "${example%\:*}";输出:456(作为单行测试,列表需要 for 循环)。