sed字符替换有个难题,谁帮忙想下办法啊
我用sed对文件做下字符替换,日志文件格式如下:
test.log:
logfile.o: ../include/logfile.c ../include/logfile.h
现在想用sed把test.log替换成:
../include/logfile.o : ../include/logfile.c ../include/logfile.h
也就是把logfile.o 替换成../include/logfile.o
当然了这里logfile.o这些在实际应用都是变量来着,所以也可能是test.o,ce.o等其他值,这点要注意.
我自己写了个替换脚本:
#!/bin/sh
echo sed log
str=../include/logfile.o #变量带 斜杠会导致脚本执行失败-怎么办
cat test.log|sed 's/^.*:/'${str}':/' >log.$$$$;
这里会有问题是str变量这里是带斜杠的, 所以在sed就提示出错了,问题是变量str我是自动生成的,无法手到改成..\\/include\\/logfile.o.
有什么办法让str变量如果查看到了/就变成\\/ ,
这里实际应用有些有'/'有些没有,所以注意只能用替换法,而不要用分割字符的方法,那样没有通用性.