sed字符替换有个难题,谁帮忙想下办法啊

yanjinbin0 2010-05-26 11:35:18
我用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变量如果查看到了/就变成\\/ ,
这里实际应用有些有'/'有些没有,所以注意只能用替换法,而不要用分割字符的方法,那样没有通用性.

...全文
171 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanjinbin0 2010-05-26
  • 打赏
  • 举报
回复
分割符 自定义 OK .谢谢。
lylm 2010-05-26
  • 打赏
  • 举报
回复
看这个
echo "logfile.o: ../include/logfile.c ../include/logfile.h"|sed 's/\(.*\.o\)/\.\.\/include\/\1/'
mymtom 2010-05-26
  • 打赏
  • 举报
回复
上面的少改了一个
cat test.log | sed 's,^.*:,'${str}':,'
mymtom 2010-05-26
  • 打赏
  • 举报
回复
分隔符可以自己定义的,这里用逗号就好了。
cat test.log | sed 's,^.*:/'${str}':,'

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧