![검색 패턴을 통해 줄 구분 기호로 -로 구분된 콘텐츠를 제거합니다.](https://linux55.com/image/173226/%EA%B2%80%EC%83%89%20%ED%8C%A8%ED%84%B4%EC%9D%84%20%ED%86%B5%ED%95%B4%20%EC%A4%84%20%EA%B5%AC%EB%B6%84%20%EA%B8%B0%ED%98%B8%EB%A1%9C%20-%EB%A1%9C%20%EA%B5%AC%EB%B6%84%EB%90%9C%20%EC%BD%98%ED%85%90%EC%B8%A0%EB%A5%BC%20%EC%A0%9C%EA%B1%B0%ED%95%A9%EB%8B%88%EB%8B%A4..png)
다음 입력이 포함된 파일이 있습니다.
----------
ID: alternatives_create_editor
Function: alternatives.install
Name: editor
Result: None
Comment: Alternative will be set for editor to /usr/bin/vim.basic with priority 100
Started: 10:45:49.115459
Duration: 0.78 ms
Changes:
----------
ID: hosts_1
Function: host.present
Name: ip6-allnodes
Result: None
Comment: Host ip6-allnodes (ff02::1) already present
Started: 10:45:49.127667
Duration: 1.117 ms
Changes:
----------
ID: sfrsyslog_fix_logrotate
Function: file.managed
Name: /etc/logrotate.d/rsyslog
Result: None
Comment: The file /etc/logrotate.d/rsyslog is set to be changed
Started: 10:45:50.771278
Duration: 12.751 ms
Changes:
----------
내 목표는 이미 존재하는 전체 Comment: 포함 섹션을 제거하는 것입니다.
나는 노력했다 grep -B 5 -A 4 'already present' FILE1 > FILE2
.
그 다음에,
diff -y FILE1 FILE2 | grep '<' | sed 's/
이 출력에서는 아래와 같은 결과를 얻습니다.
ID: alternatives_create_editor
Function: alternatives.install
Name: editor
Result: None
Comment: Alternative will be set for editor to /usr/bin/
Started: 10:45:49.115459
Duration: 0.78 ms
Changes:
----------
ID: sfrsyslog_fix_logrotate
Function: file.managed
Name: /etc/logrotate.d/rsyslog
Result: None
Comment: The file /etc/logrotate.d/rsyslog is set to be
Started: 10:45:50.771278
Duration: 12.751 ms
Changes:
----------
문제는 Comment: section
완전한 라인이 제공되지 않는다는 것입니다. 도와주세요. 감사해요
답변1
더 간단한 방법은 단일 GNU awk 명령을 사용하는 것입니다.
awk 'BEGIN{RS=ORS="----------\n"};!/Comment:[^\n]*already present/' file
레코드 구분 기호를 대시 10개로 설정하고 Comment:[^\n]*already present
대시가 포함된 경우 레코드를 억제합니다. [^\n]*
"개행이 아님"을 의미하므로 레코드에 already present
in 이 있지만 Changes:
in 을 포함하지 않는 경우 해당 레코드는 표시되지 않습니다 Comment:
.
산출:
----------
ID: alternatives_create_editor
Function: alternatives.install
Name: editor
Result: None
Comment: Alternative will be set for editor to /usr/bin/vim.basic with priority 100
Started: 10:45:49.115459
Duration: 0.78 ms
Changes:
----------
ID: sfrsyslog_fix_logrotate
Function: file.managed
Name: /etc/logrotate.d/rsyslog
Result: None
Comment: The file /etc/logrotate.d/rsyslog is set to be changed
Started: 10:45:50.771278
Duration: 12.751 ms
Changes:
----------
GNU awk가 없으면 POSIX가 다중 문자 레코드 구분 기호의 동작을 정의하지 않기 때문에 위의 내용이 작동하지 않을 수 있습니다. 그래서 나는 다음과 같은 휴대용 대안을 남겼습니다.
awk '/Comment:.*already present/{p=1}
/^-{10}$/{
if(p!=1){printf "%s",lines}
p=0
lines=""
}
{lines=lines $0 RS}
' file
답변2
또 다른 awk
해결책:
awk 'BEGIN{RS=ORS="----------\n"} $0 !~ /already present/' file