Linux sed 命令英文全名 Stream Editor(文本流编辑),它处理文件时,把当前处理的行存储在临时缓冲区中,接着用 sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非我们使用重定向存储输出。
sed 命令主要用来编辑一个或多个文件,它可以将数据行进行替换、删除、新增、选取等特定工作,简化对文件的反复操作,编写转换程序等复杂工作。
语法
命令形式
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
命令模式
- s/regexp/replacement/
- sed 字符串匹配模式,其中
s
表示替换,三根斜线之间是替换的样式(可以是正则表达式),特殊字符需要使用反斜线\
进行转义。
常用命令
sed 命令指定位置匹配替换
如何用 sed 命令替换第一个或指定第几个或全匹配字符串,具体示例如下:
假设有如下文件内容:
[demo@Linux rank_13]$ head -n 2 train.libsvm
0 qid:1 0:97.0 1:1.0 2:12.0 3:0.0 4:237.0 5:2.0 7:0.0 8:1.0 9:19.0 10:1.0 12:3.5 13:14.581183 14:1.0 15:1.0 16:2.0 17:0.0 18:1.0 20:1.0 21:2.0 22:0.0 23:0.0 24:0.0 25:1.0 26:0.0 27:0.0 29:0.0 30:0.0 31:218.0 32:66.0 33:1.0 34:0.0 35:0.01 37:3.583333 39:0.0 40:1.0 41:1.0 42:1.0 44:0.0 46:0.0 47:0.0 48:2.0 50:0.01 51:0.0 52:0.01 53:0.01 54:0.0 56:0.01 59:0.0 61:1.0 62:1.0 64:0.0 65:0.0 67:1.0 68:0.0 116:1.0 119:1.0 120:1.0 122:1.0 125:0.0 143:0.0 144:42.0 145:0.0 146:0.0 147:0.0 148:127.19119 185:0.01 186:0.0 187:0.0 188:0.08285783 189:16.477295 190:4.0 191:2.0 231:0.01 232:1.0 234:1.0 236:0.0 237:384.0 238:0.0 239:0.0 240:0.0 241:1.0 242:43.0 243:0.01
0 qid:3 0:68.0 2:340.0 3:0.022931 4:4442.0 5:80.0 6:3.115746 7:5.0 9:23.0 10:1.0 12:0.0 13:7.5371222 16:80.0 17:1.0 18:1.0 20:1.0 21:0.0 22:0.0 23:0.022931 24:5.0 25:1.0 26:0.053191 27:1.0 29:0.0 30:6.0 31:29.0 32:8.0 34:0.026989 35:0.036223 36:1.0 37:0.017647 39:5.0 40:1.0 41:1.0 42:1.0 44:0.0625 46:0.0 47:0.0 48:94.0 50:0.030053 51:0.0 52:0.166667 53:0.2 54:5.0 56:0.2 59:6.0 61:1.0 64:0.0 65:1.0 68:0.0 71:1.0 77:1.0 81:1.0 84:1.0 90:1.0 92:1.0 116:1.0 119:1.0 120:1.0 122:1.0 125:5.0 143:1.0 144:0.0 145:0.0 146:0.0 147:5.0 148:63.563557 157:1.0 185:0.030053 186:0.026989 187:1.0 188:0.08557996 189:2.3966966 190:13.0 191:94.0 198:1.0 201:1.0 204:1.0 205:1.0 231:0.166667 236:0.0 237:3730.0 238:0.0 239:0.0625 240:0.053191 241:1.0 242:6.0 243:0.036223 297:1.0 298:1.0
替换第一个冒号为空格,命令如下:
[demo@Linux rank_13]$ sed 's/:/ /' test.libsvm | head -n 2
0 qid 93736 0:151.0 2:20.0 3:0.006091 4:122.0 5:12.0 7:3.0 8:1.0 9:11.0 10:1.0 12:0.0 13:6.638686 16:135.0 17:0.0 18:1.0 20:1.0 21:1.0 22:0.0 23:0.01371 24:6.0 25:1.0 26:0.076923 27:0.0 29:0.0 30:7.0 31:16.0 32:81.0 34:0.014865 35:0.0 37:0.8 39:1.0 41:1.0 42:1.0 44:0.083333 46:0.0 47:5.0 48:168.0 50:0.0 51:0.0 52:0.0 53:0.0 54:1.0 56:0.0 58:1.0 59:7.0 61:1.0 64:0.0 65:0.0 68:0.0 69:1.0 119:1.0 120:1.0 122:1.0 125:6.0 135:1.0 141:1.0 143:0.0 144:0.0 145:1.0 146:0.0 147:3.0 148:61.332012 185:0.0 186:0.007586 187:0.0 188:0.0 189:3.05332 190:23.0 191:13.0 231:0.0 236:0.0 237:1694.0 239:0.022222 240:0.017857 241:1.0 242:16.0 243:0.0 274:1.0 400:1.0 764:1.0
0 qid 93771 0:71.0 2:608.0 3:0.040505 4:5094.0 5:2881.0 7:161.0 8:1.0 9:14.0 10:1.0 11:1.0 12:0.0 13:30.773151 16:2881.0 17:52.0 18:1.0 19:1.0 20:1.0 21:1.0 22:0.006579 23:0.040505 24:636.0 25:1.0 26:0.046768 27:0.0 29:0.0 30:793.0 31:4.0 32:6.0 34:0.048072 35:0.057395 37:0.006579 39:161.0 41:1.0 42:1.0 44:0.055883 46:0.0 47:7.0 48:3806.0 50:0.050354 51:0.0 52:0.065574 53:0.075472 54:178.0 56:0.075472 59:793.0 61:1.0 64:0.0 65:48.0 68:0.0 84:1.0 119:1.0 120:1.0 122:1.0 125:636.0 136:1.0 143:48.0 144:0.0 145:0.0 146:0.0 147:178.0 148:271.5694 170:1.0 180:1.0 185:0.050354 186:0.048072 187:52.0 188:0.15997131 189:30.894512 190:18.0 191:3806.0 231:0.065574 236:4.0 237:6668.0 239:0.055883 240:0.046768 242:4.0 243:0.057395 274:1.0 397:1.0 3133:1.0 3234:1.0 3347:1.0 13291:1.0 13404:1.0
也可以在最后一个 / 后指定第几个匹配位置,如下和上等同:
[demo@Linux rank_13]$ sed 's/:/ /1' test.libsvm | head -n 2
0 qid 93736 0:151.0 2:20.0 3:0.006091 4:122.0 5:12.0 7:3.0 8:1.0 9:11.0 10:1.0 12:0.0 13:6.638686 16:135.0 17:0.0 18:1.0 20:1.0 21:1.0 22:0.0 23:0.01371 24:6.0 25:1.0 26:0.076923 27:0.0 29:0.0 30:7.0 31:16.0 32:81.0 34:0.014865 35:0.0 37:0.8 39:1.0 41:1.0 42:1.0 44:0.083333 46:0.0 47:5.0 48:168.0 50:0.0 51:0.0 52:0.0 53:0.0 54:1.0 56:0.0 58:1.0 59:7.0 61:1.0 64:0.0 65:0.0 68:0.0 69:1.0 119:1.0 120:1.0 122:1.0 125:6.0 135:1.0 141:1.0 143:0.0 144:0.0 145:1.0 146:0.0 147:3.0 148:61.332012 185:0.0 186:0.007586 187:0.0 188:0.0 189:3.05332 190:23.0 191:13.0 231:0.0 236:0.0 237:1694.0 239:0.022222 240:0.017857 241:1.0 242:16.0 243:0.0 274:1.0 400:1.0 764:1.0
0 qid 93771 0:71.0 2:608.0 3:0.040505 4:5094.0 5:2881.0 7:161.0 8:1.0 9:14.0 10:1.0 11:1.0 12:0.0 13:30.773151 16:2881.0 17:52.0 18:1.0 19:1.0 20:1.0 21:1.0 22:0.006579 23:0.040505 24:636.0 25:1.0 26:0.046768 27:0.0 29:0.0 30:793.0 31:4.0 32:6.0 34:0.048072 35:0.057395 37:0.006579 39:161.0 41:1.0 42:1.0 44:0.055883 46:0.0 47:7.0 48:3806.0 50:0.050354 51:0.0 52:0.065574 53:0.075472 54:178.0 56:0.075472 59:793.0 61:1.0 64:0.0 65:48.0 68:0.0 84:1.0 119:1.0 120:1.0 122:1.0 125:636.0 136:1.0 143:48.0 144:0.0 145:0.0 146:0.0 147:178.0 148:271.5694 170:1.0 180:1.0 185:0.050354 186:0.048072 187:52.0 188:0.15997131 189:30.894512 190:18.0 191:3806.0 231:0.065574 236:4.0 237:6668.0 239:0.055883 240:0.046768 242:4.0 243:0.057395 274:1.0 397:1.0 3133:1.0 3234:1.0 3347:1.0 13291:1.0 13404:1.0
如果只想替换第二个匹配的冒号,命令如下:
[demo@Linux rank_13]$ sed 's/:/ /2' test.libsvm | head -n 2
0 qid:93736 0 151.0 2:20.0 3:0.006091 4:122.0 5:12.0 7:3.0 8:1.0 9:11.0 10:1.0 12:0.0 13:6.638686 16:135.0 17:0.0 18:1.0 20:1.0 21:1.0 22:0.0 23:0.01371 24:6.0 25:1.0 26:0.076923 27:0.0 29:0.0 30:7.0 31:16.0 32:81.0 34:0.014865 35:0.0 37:0.8 39:1.0 41:1.0 42:1.0 44:0.083333 46:0.0 47:5.0 48:168.0 50:0.0 51:0.0 52:0.0 53:0.0 54:1.0 56:0.0 58:1.0 59:7.0 61:1.0 64:0.0 65:0.0 68:0.0 69:1.0 119:1.0 120:1.0 122:1.0 125:6.0 135:1.0 141:1.0 143:0.0 144:0.0 145:1.0 146:0.0 147:3.0 148:61.332012 185:0.0 186:0.007586 187:0.0 188:0.0 189:3.05332 190:23.0 191:13.0 231:0.0 236:0.0 237:1694.0 239:0.022222 240:0.017857 241:1.0 242:16.0 243:0.0 274:1.0 400:1.0 764:1.0
0 qid:93771 0 71.0 2:608.0 3:0.040505 4:5094.0 5:2881.0 7:161.0 8:1.0 9:14.0 10:1.0 11:1.0 12:0.0 13:30.773151 16:2881.0 17:52.0 18:1.0 19:1.0 20:1.0 21:1.0 22:0.006579 23:0.040505 24:636.0 25:1.0 26:0.046768 27:0.0 29:0.0 30:793.0 31:4.0 32:6.0 34:0.048072 35:0.057395 37:0.006579 39:161.0 41:1.0 42:1.0 44:0.055883 46:0.0 47:7.0 48:3806.0 50:0.050354 51:0.0 52:0.065574 53:0.075472 54:178.0 56:0.075472 59:793.0 61:1.0 64:0.0 65:48.0 68:0.0 84:1.0 119:1.0 120:1.0 122:1.0 125:636.0 136:1.0 143:48.0 144:0.0 145:0.0 146:0.0 147:178.0 148:271.5694 170:1.0 180:1.0 185:0.050354 186:0.048072 187:52.0 188:0.15997131 189:30.894512 190:18.0 191:3806.0 231:0.065574 236:4.0 237:6668.0 239:0.055883 240:0.046768 242:4.0 243:0.057395 274:1.0 397:1.0 3133:1.0 3234:1.0 3347:1.0 13291:1.0 13404:1.0
如果替换全部的冒号为空格,用 g 指定,表示全局(global)生效,具体示例如下:
[demo@Linux rank_13]$ sed 's/:/ /g' test.libsvm | head -n 2
0 qid 93736 0 151.0 2 20.0 3 0.006091 4 122.0 5 12.0 7 3.0 8 1.0 9 11.0 10 1.0 12 0.0 13 6.638686 16 135.0 17 0.0 18 1.0 20 1.0 21 1.0 22 0.0 23 0.01371 24 6.0 25 1.0 26 0.076923 27 0.0 29 0.0 30 7.0 31 16.0 32 81.0 34 0.014865 35 0.0 37 0.8 39 1.0 41 1.0 42 1.0 44 0.083333 46 0.0 47 5.0 48 168.0 50 0.0 51 0.0 52 0.0 53 0.0 54 1.0 56 0.0 58 1.0 59 7.0 61 1.0 64 0.0 65 0.0 68 0.0 69 1.0 119 1.0 120 1.0 122 1.0 125 6.0 135 1.0 141 1.0 143 0.0 144 0.0 145 1.0 146 0.0 147 3.0 148 61.332012 185 0.0 186 0.007586 187 0.0 188 0.0 189 3.05332 190 23.0 191 13.0 231 0.0 236 0.0 237 1694.0 239 0.022222 240 0.017857 241 1.0 242 16.0 243 0.0 274 1.0 400 1.0 764 1.0
0 qid 93771 0 71.0 2 608.0 3 0.040505 4 5094.0 5 2881.0 7 161.0 8 1.0 9 14.0 10 1.0 11 1.0 12 0.0 13 30.773151 16 2881.0 17 52.0 18 1.0 19 1.0 20 1.0 21 1.0 22 0.006579 23 0.040505 24 636.0 25 1.0 26 0.046768 27 0.0 29 0.0 30 793.0 31 4.0 32 6.0 34 0.048072 35 0.057395 37 0.006579 39 161.0 41 1.0 42 1.0 44 0.055883 46 0.0 47 7.0 48 3806.0 50 0.050354 51 0.0 52 0.065574 53 0.075472 54 178.0 56 0.075472 59 793.0 61 1.0 64 0.0 65 48.0 68 0.0 84 1.0 119 1.0 120 1.0 122 1.0 125 636.0 136 1.0 143 48.0 144 0.0 145 0.0 146 0.0 147 178.0 148 271.5694 170 1.0 180 1.0 185 0.050354 186 0.048072 187 52.0 188 0.15997131 189 30.894512 190 18.0 191 3806.0 231 0.065574 236 4.0 237 6668.0 239 0.055883 240 0.046768 242 4.0 243 0.057395 274 1.0 397 1.0 3133 1.0 3234 1.0 3347 1.0 13291 1.0 13404 1.0