shell脚本 - 从数组中替换变量

时间:2014-04-23 08:09:56

标签: shell replace

脚本产生以下输出:

MyProgram down : Computer7 Network 31 N_ID 1 at : 11-Apr-2014 14:56:06.22
MyProgram up   : Computer7 Network 31 N_ID 1 at : 11-Apr-2014 14:57:06.22
MyProgram down : Computer7 Network 32 N_ID 1 at : 11-Apr-2014 14:57:06.40
MyProgram down : Computer8 Network 32 N_ID 2 at : 11-Apr-2014 14:57:06.40
MyProgram up   : Computer7 Network 32 N_ID 1 at : 11-Apr-2014 14:57:06.43
MyProgram up   : Computer8 Network 32 N_ID 2 at : 11-Apr-2014 14:57:06.43
MyProgram down : Computer8 Network 19 N_ID 2 at : 12-Apr-2014 01:59:28.46
MyProgram up   : Computer8 Network 19 N_ID 2 at : 12-Apr-2014 01:59:29.46

但是,我需要批量替换某些字符串,例如

Computer7 Network 31 = 'Harbor1'
Computer7 Network 32 = 'Matrix7'
Computer8 Network 19 = 'Romulos1'
Computer8 Network 32 = 'Synergy20'
...

实现以下输出的简短且高效的解决方案是什么?

MyProgram down : Harbor1 N_ID 1 at : 11-Apr-2014 14:56:06.22
MyProgram up   : Harbor1 N_ID 1 at : 11-Apr-2014 14:57:06.22
MyProgram down : Matrix7 N_ID 1 at : 11-Apr-2014 14:57:06.40
MyProgram down : Synergy20 N_ID 2 at : 11-Apr-2014 14:57:06.40
MyProgram up   : Matrix7 N_ID 1 at : 11-Apr-2014 14:57:06.43
MyProgram up   : Synergy20 N_ID 2 at : 11-Apr-2014 14:57:06.43
MyProgram down : Romulos1 N_ID 2 at : 12-Apr-2014 01:59:28.46
MyProgram up   : Romulos1 N_ID 2 at : 12-Apr-2014 01:59:29.46

在映射新输出之后,我可以如何对同一程序的事件进行分组,例如:

MyProgram down/up : Harbor1 N_ID 1 Down 14:56:06.22 - Up 14:57:06.22 on 11-Apr-2014
MyProgram down/up : Matrix7 N_ID 1 Down 14:57:06.40 - Up 14:57:06.43 on 11-Apr-2014
MyProgram down/up : Synergy20 N_ID 2 Down 14:57:06.40 - Up 14:57:06.43 on 11-Apr-2014
MyProgram down/up : Romulos1 N_ID 2 Down 01:59:28.46 - Up 01:59:29.46 on 12-Apr-2014

1 个答案:

答案 0 :(得分:0)

您可以使用sed -f

$ cat mappings
s/Computer7 Network 31/Harbor1/
s/Computer7 Network 32/Matrix7/
s/Computer8 Network 19/Romulos1/
s/Computer8 Network 32/Synergy20/

$ sed -f mappings file
MyProgram down : Harbor1 N_ID 1 at : 11-Apr-2014 14:56:06.22
MyProgram up   : Harbor1 N_ID 1 at : 11-Apr-2014 14:57:06.22
MyProgram down : Matrix7 N_ID 1 at : 11-Apr-2014 14:57:06.40
MyProgram down : Synergy20 N_ID 2 at : 11-Apr-2014 14:57:06.40
MyProgram up   : Matrix7 N_ID 1 at : 11-Apr-2014 14:57:06.43
MyProgram up   : Synergy20 N_ID 2 at : 11-Apr-2014 14:57:06.43
MyProgram down : Romulos1 N_ID 2 at : 12-Apr-2014 01:59:28.46
MyProgram up   : Romulos1 N_ID 2 at : 12-Apr-2014 01:59:29.46

如果您发现使用sed表达式准备映射文件很麻烦,您可以使用任意格式并使用其他sedprocess substitution来准备-f的输入,例如:

$ cat mappings
Computer7 Network 31=Harbor1
Computer7 Network 32=Matrix7
Computer8 Network 19=Romulos1
Computer8 Network 32=Synergy20

$ sed -f <(sed 's#\(.*\)=\(.*\)#s/\1/\2/#g' mappings) file
MyProgram down : Harbor1 N_ID 1 at : 11-Apr-2014 14:56:06.22
MyProgram up   : Harbor1 N_ID 1 at : 11-Apr-2014 14:57:06.22
MyProgram down : Matrix7 N_ID 1 at : 11-Apr-2014 14:57:06.40
MyProgram down : Synergy20 N_ID 2 at : 11-Apr-2014 14:57:06.40
MyProgram up   : Matrix7 N_ID 1 at : 11-Apr-2014 14:57:06.43
MyProgram up   : Synergy20 N_ID 2 at : 11-Apr-2014 14:57:06.43
MyProgram down : Romulos1 N_ID 2 at : 12-Apr-2014 01:59:28.46
MyProgram up   : Romulos1 N_ID 2 at : 12-Apr-2014 01:59:29.46