我已经编译了一些代码,但有很多错误,我想创建一个表来显示一些内容:源文件的路径、行号和未定义的引用。使用简单的 awk 并打印 $1,$2 等来挖掘第一个和第二个并不是什么大问题,但未定义的引用是有问题的,因为它出现在输出中的不同位置。
这是我试图用我想编写的脚本来解析的错误日志的片段。
out/target/product/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h:408:错误:未定义引用 'fishy_proto::Service::CopyFrom(fishy_proto::Service常量&)' base/fishy/fishy/impl/service.cpp:448:错误:未定义对“fishy_proto::Service::Service()”的引用 base/fishy/fishy/impl/service.cpp:489:错误:未定义对“fishy_proto::Service::~Service()”的引用 base/fishy/fishy/impl/service.cpp:489:错误:未定义对“fishy_proto::Service::~Service()”的引用 base/fishy/fishy/impl/service_registry.cpp:51:错误:未定义对“fishy_proto::ServiceList::ServiceList()”的引用 base/fishy/fishy/impl/service_registry.cpp:51:错误:未定义对“fishy_proto::ServiceList::~ServiceList()”的引用 base/fishy/fishy/impl/service_registry.cpp:51:错误:未定义对“fishy_proto::ServiceList::~ServiceList()”的引用 base/fishy/fishy/impl/service_registry.cpp:51:错误:未定义对“fishy_proto::ServiceList::~ServiceList()”的引用 base/fishy/fishy/impl/service_registry.cpp:51:错误:未定义对“fishy_proto::ServiceList::~ServiceList()”的引用 base/fishy/fishy/impl/service_registry.cpp:279:错误:未定义对“fishy_proto::Request::Request()”的引用 base/fishy/fishy/impl/service_registry.cpp:319:错误:未定义对“fishy_proto::Request::~Request()”的引用 base/fishy/fishy/impl/service_registry.cpp:319:错误:未定义对“fishy_proto::Request::~Request()”的引用 base/fishy/fishy/impl/service_registry.cpp:722:错误:未定义对“fishy_proto::Request::Request()”的引用 out/target/product/phaedra/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h:72:错误:未定义对“fishy_proto::Request_Type_descriptor()”的引用 base/fishy/fishy/impl/service_registry.cpp:762:错误:未定义对“fishy_proto::Request::~Request()”的引用 base/fishy/fishy/impl/service_registry.cpp:762:错误:未定义对“fishy_proto::Request::~Request()”的引用 base/fishy/fishy/impl/service_registry.cpp:463:错误:未定义对“fishy_proto::Service::Service()”的引用 base/fishy/fishy/impl/service_registry.cpp:500:错误:未定义对“fishy_proto::Service::~Service()”的引用 base/fishy/fishy/impl/service_registry.cpp:500:错误:未定义对“fishy_proto::Service::~Service()”的引用 external/protobuf/src/google/protobuf/arena.h:649:错误:未定义对“fishy_proto::ServiceGroup::ServiceGroup()”的引用 external/protobuf/src/google/protobuf/arena.h:309:错误:未定义对“fishy_proto::ServiceGroup::ServiceGroup()”的引用 external/protobuf/src/google/protobuf/arena.h:649:错误:未定义对“fishy_proto::Service::Service()”的引用 external/protobuf/src/google/protobuf/arena.h:309:错误:未定义对“fishy_proto::Service::Service()”的引用
正如您所看到的,与其他行相比,第 1 行和第 14 行在不同位置显示了未定义的引用。
有没有一种方法可以运行 awk,它会忽略确切的位置,但仍会带来未定义引用的值?
答案1
您的示例输出表明“未定义的引用”将始终是awk
一行的最后一个字段。在 中awk
,NF
将是字段的数量,并且$NF
将是其数量为“字段数量”的字段,即。最后一个字段,这就是您所要求的。
awk '{print "undefined reference = " $NF}'
答案2
源文件的路径、行号和未定义的引用
sed
解决方案:
sed -E 's/^([^:]+):([0-9]+): error: undefined reference to (.*)/\1 \2 \3/' file
输出:
out/target/product/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h 408 'fishy_proto::Service::CopyFrom(fishy_proto::Service const&)'
base/fishy/fishy/impl/service.cpp 448 'fishy_proto::Service::Service()'
base/fishy/fishy/impl/service.cpp 489 'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service.cpp 489 'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 279 'fishy_proto::Request::Request()'
base/fishy/fishy/impl/service_registry.cpp 319 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 319 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 722 'fishy_proto::Request::Request()'
out/target/product/phaedra/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h 72 'fishy_proto::Request_Type_descriptor()'
base/fishy/fishy/impl/service_registry.cpp 762 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 762 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 463 'fishy_proto::Service::Service()'
base/fishy/fishy/impl/service_registry.cpp 500 'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service_registry.cpp 500 'fishy_proto::Service::~Service()'
external/protobuf/src/google/protobuf/arena.h 649 'fishy_proto::ServiceGroup::ServiceGroup()'
external/protobuf/src/google/protobuf/arena.h 309 'fishy_proto::ServiceGroup::ServiceGroup()'
external/protobuf/src/google/protobuf/arena.h 649 'fishy_proto::Service::Service()'
external/protobuf/src/google/protobuf/arena.h 309 'fishy_proto::Service::Service()'
答案3
如果你不介意使用 perl:
perl -pe 's/^(.*?):(\d+): error: undefined reference to (.*)/\1\t\2\t\3/g'
您的示例的输出将如下所示:
out/target/product/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h 408 'fishy_proto::Service::CopyFrom(fishy_proto::Service const&)'
base/fishy/fishy/impl/service.cpp 448 'fishy_proto::Service::Service()'
base/fishy/fishy/impl/service.cpp 489 'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service.cpp 489 'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 51 'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp 279 'fishy_proto::Request::Request()'
base/fishy/fishy/impl/service_registry.cpp 319 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 319 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 722 'fishy_proto::Request::Request()'
out/target/product/phaedra/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h 72 'fishy_proto::Request_Type_descriptor()'
base/fishy/fishy/impl/service_registry.cpp 762 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 762 'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp 463 'fishy_proto::Service::Service()'
base/fishy/fishy/impl/service_registry.cpp 500 'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service_registry.cpp 500 'fishy_proto::Service::~Service()'
external/protobuf/src/google/protobuf/arena.h 649 'fishy_proto::ServiceGroup::ServiceGroup()'
external/protobuf/src/google/protobuf/arena.h 309 'fishy_proto::ServiceGroup::ServiceGroup()'
external/protobuf/src/google/protobuf/arena.h 649 'fishy_proto::Service::Service()'
external/protobuf/src/google/protobuf/arena.h 309 'fishy_proto::Service::Service()'
答案4
我已经使用 sed 通过以下方法完成了
sed -ne "s/\(.*error:\)\(.*reference to \)\('.*'\)/\1\3/p" inputfile
输出
out/target/product/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h:408: error:'fishy_proto::Service::CopyFrom(fishy_proto::Service const&)'
base/fishy/fishy/impl/service.cpp:448: error:'fishy_proto::Service::Service()'
base/fishy/fishy/impl/service.cpp:489: error:'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service.cpp:489: error:'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service_registry.cpp:51: error:'fishy_proto::ServiceList::ServiceList()'
base/fishy/fishy/impl/service_registry.cpp:51: error:'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp:51: error:'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp:51: error:'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp:51: error:'fishy_proto::ServiceList::~ServiceList()'
base/fishy/fishy/impl/service_registry.cpp:279: error:'fishy_proto::Request::Request()'
base/fishy/fishy/impl/service_registry.cpp:319: error:'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp:319: error:'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp:722: error:'fishy_proto::Request::Request()'
out/target/product/phaedra/gen/SHARED_LIBRARIES/libfishy_proto_intermediates/proto/base/fishy/fishy/prefix/fishy_proto/service_registry.pb.h:72: error:'fishy_proto::Request_Type_descriptor()'
base/fishy/fishy/impl/service_registry.cpp:762: error:'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp:762: error:'fishy_proto::Request::~Request()'
base/fishy/fishy/impl/service_registry.cpp:463: error:'fishy_proto::Service::Service()'
base/fishy/fishy/impl/service_registry.cpp:500: error:'fishy_proto::Service::~Service()'
base/fishy/fishy/impl/service_registry.cpp:500: error:'fishy_proto::Service::~Service()'
external/protobuf/src/google/protobuf/arena.h:649: error:'fishy_proto::ServiceGroup::ServiceGroup()'
external/protobuf/src/google/protobuf/arena.h:309: error:'fishy_proto::ServiceGroup::ServiceGroup()'
external/protobuf/src/google/protobuf/arena.h:649: error:'fishy_proto::Service::Service()'
external/protobuf/src/google/protobuf/arena.h:309: error:'fishy_proto::Service::Service()'