LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 914|回复: 3

问个文本替换的问题

[复制链接]
发表于 2006-8-15 01:50:45 | 显示全部楼层 |阅读模式
想做这样一件事情:
有文本如下
  1.    1      0.77662      0.16000 |    1^+ 1^-
复制代码
替换0.77662为其他的数,如0.88888,其他保持不变
  1.    1      0.88888      0.16000 |    1^+ 1^-
复制代码
如何实现?

我用awk '{$2 ='"0.88888"';print}'`,它把中间的空格删掉只有一个了,成了下面的样子
  1. 1 0.88888 0.16000 | 1^+ 1^-
复制代码
发表于 2006-8-15 23:15:07 | 显示全部楼层
  1. awk 'BEGIN{FS="";OFS=""};{$8="0.88888";$9="";$10=$9;$11=$9;$12=$9;$13=$9;$14=$9;print}'
复制代码
This is a ugly way (calc the char) but given the fact that the FS in the orig string isn't consistent, I can't think of another by using awk. Also, when you assign value to any of the fields, awk will restore the default OFS. Using sed will be a hell lot easier.
回复 支持 反对

使用道具 举报

发表于 2006-8-25 19:00:01 | 显示全部楼层
使用函数 sub 实现:

1. 替换所有'0.77662' :
  1. awk '{sub(/0.77662/, "0.88888"); print}'
复制代码


2. 仅替换第二列的'0.77662' :
  1. awk '{ if($2 == 0.77662) sub(/0.77662/, "0.88888"); print}'
复制代码

此时的不足是: 下行中的'0.77662'都会被替换掉:
  1. 1      0.77662      0.77662      |    1^+ 1^-
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-8-26 20:29:08 | 显示全部楼层
sed 's/77662/88888/g' filename
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表