如何使用 shell 命令在每个匹配的字符串后添加新文本?

如何使用 shell 命令在每个匹配的字符串后添加新文本?

输入:

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

相关内容