쉘 명령을 사용하여 일치하는 각 문자열 뒤에 새 텍스트를 추가하는 방법은 무엇입니까?

쉘 명령을 사용하여 일치하는 각 문자열 뒤에 새 텍스트를 추가하는 방법은 무엇입니까?

입력하다:

Job name: ns, Job ID: 2312, Status: ODB_ACTIAVTION SUCCESSFUL
Job name: ps, Job ID: 3353, Status: ODB_ACTIAVTION SUCCESSFUL
Job name: pm, Job ID: 1265, Status: ODB_ACTIAVTION SUCCESSFUL
Job name: np, Job ID: 7885, Status: ODB_ACTIAVTION SUCCESSFUL
Job name: as, Job ID: 3215, Status: ODB_ACTIAVTION UNSUCCESSFUL

산출:

Job name: ns, Job ID: 2312, Status: ODB_ACTIAVTION SUCCESSFUL
Changes has been updated to all servers
Job name: ps, Job ID: 3353, Status: ODB_ACTIAVTION SUCCESSFUL
Changes has been updated to all servers
Job name: pm, Job ID: 1265, Status: ODB_ACTIAVTION SUCCESSFUL
Changes has been updated to all servers
Job name: np, Job ID: 7885, Status: ODB_ACTIAVTION SUCCESSFUL
Changes has been updated to all servers
Job name: as, Job ID: 3215, Status: ODB_ACTIAVTION UNSUCCESSFUL
No change

Changes has been updated to all servers문자열과 일치하는 모든 줄에 이 텍스트를 추가 ODB_ACTIAVTION SUCCESSFUL하고 No change문자열과 일치하는 줄 뒤에 텍스트를 추가하고 싶습니다 ODB_ACTIAVTION UNSUCCESSFUL.

답변1

awk '/ODB_ACTIAVTION SUCCESSFUL/ {printf "%s\nChanges has been updated to all servers\n", $0}
     /ODB_ACTIAVTION UNSUCCESSFUL/ {printf "%s\nNo change\n", $0}' file1 > file2

답변2

그리고 awk:

awk '
/ODB_ACTIAVTION SUCCESSFUL/{printf "%s\nChanges has been updated to all servers\n", $0}
/ODB_ACTIAVTION UNSUCCESSFUL/{printf "%s\nNo change\n", $0}
' <in >out

그리고 sed:

sed -e '
  /ODB_ACTIAVTION SUCCESSFUL/a\
  Changes has been updated to all servers
  /ODB_ACTIAVTION UNSUCCESSFUL/a\
  No change
' <in >out

관련 정보