![awk를 사용하여 동적 위치에서 데이터 가져오기](https://linux55.com/image/128065/awk%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%EB%8F%99%EC%A0%81%20%EC%9C%84%EC%B9%98%EC%97%90%EC%84%9C%20%EB%8D%B0%EC%9D%B4%ED%84%B0%20%EA%B0%80%EC%A0%B8%EC%98%A4%EA%B8%B0.png)
일부 코드를 컴파일했지만 오류가 많아 소스 파일 경로, 줄 번호, 정의되지 않은 참조 등 몇 가지 사항을 보여주는 테이블을 만들고 싶습니다. 간단한 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 via를 사용하여 그것을 해냈습니다.
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()'