sed查找并替换一个特定的数字

菩萨87

我有一个如下文件。

abc  259200000     2     3  864000000     3     5
def  86400000      2    62  864000000     3    62
efg  864000000     2   347          0     0     0
abcd 259200000     3     3          0     0     0

我需要用单词Not Exist替换任何一个0我尝试了以下操作,但都没有工作。

sed 's/[0]/Not Exist/g' data.txt > out.txt
sed 's/[^0]/Not Exist/g' data.txt > out.txt
sed 's/^[0]/Not Exist/g' data.txt > out.txt

非常感谢您的帮助。

RavinderSingh13

如果可以,请尝试以下操作awk

awk '{for(i=1;i<=NF;i++){if($i==0){$i="Not Exist"}}}{$1=$1} 1' OFS="\t" Input_file

现在也添加非单一衬里形式的解决方案。

awk '
{
  for(i=1;i<=NF;i++){
    if($i==0){
       $i="Not Exist"
    }
  }
}
{
  $1=$1
}
1
' OFS="\t"   Input_file

说明:现在也为上述代码添加了说明。

awk '
{
  for(i=1;i<=NF;i++){              ##Starting for loop from variable i=1 to value of NF(number of field) increment with 1 each time.
    if($i==0){                     ##Checking condition if value of field is 0 then do following.
       $i="Not Exist"              ##Re-making value of that field to string Not Exist now.
    }                              ##Closing if condition block now.
  }                                ##Closing for loop block here.
}
{
  $1=$1                            ##re-setting first field on current line(to make sure TAB is being made output field separator to edited lines).
}
1                                  ##Mentioning 1 means awk works on method on pattern and action. So making condition/pattern as TRUE and not mentioning any action so by default print of current line will happen.
' OFS="\t"  Input_file         ##Setting OFS as TAB and mentioning Input_file name here.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章