1

I have a file like following.

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

I need to replace any single 0 with word Not Exist. I tried following and none of them are working.

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

Much appreciate any help.

0

4 Answers 4

3

Could you please try following if ok with awk.

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

Adding a non-one liner form of solution too now.

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

Explanation: Adding explanation for above code too now.

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.
6
  • Thank you Ravinder. This is what what I expected.
    – buddhima87
    Nov 14, 2018 at 12:03
  • Can you please let me know what last 1 stands for ?
    – buddhima87
    Nov 14, 2018 at 12:06
  • @buddhima87, could you please check my explanation now and let me know if you are good here. Nov 14, 2018 at 12:15
  • 1
    Dang. We typed the same awk code, almost exactly. :)
    – ghoti
    Nov 14, 2018 at 12:21
  • 1
    Thank Ravinder. You explained it well.
    – buddhima87
    Nov 14, 2018 at 12:48
2

Here's why your three attempts so far don't work:

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

This asks sed to replace any zero character with the replacement string, including those that are part of a larger number.

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

This asks sed to replace any character which is NOT zero with the replacement string. The ^ "negates" the regex bracket expression.

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

This asks sed to replace any zero that is at the beginning of the line, since the ^ means "the null at the beginning of the line" in this context.

What you're looking for is might be expressed as follows:

sed 's/\([[:space:]]\)0\([[:space:]]\)/\1Not exist\2/g; s/\([[:space:]]\)0$/\1Not exist/' data.txt > out.txt

In this solution I'm using the space character class since I don't know whether your input file is tab or space separated. The class works with both, and retains whatever was there before.

Note that there are two sed commands here -- the first processes zeros that are have text after them, and the second processes zeros that at are the end of the line. This does make the script a bit awkward, so if you're on a more modern operating system with a sed that includes a -E option, the following might be easier to read:

sed -E 's/([[:space:]])0([[:space:]]|$)/\1Not exist\2/g' data.txt > out.txt

This takes advantage of the fact that in ERE, an "atom" can have multiple "branches", separated by an or bar (|). For more on this, man re_format.

Note that sed is probably not the best tool for this. Processing fields is usually best done with awk. I can't improve on @RavinderSingh13's awk solution, so you should use that if awk is an option.

Of course, your formatting is going to be wonky with almost any option.

1
  • Thank you @ghoti. This helped me to shape up my final output.
    – buddhima87
    Jan 2, 2019 at 16:15
1

I assume the columns are separated by white-space characters, then:

When using sed, you need to search for a lonely zero, that is zero "enclosed" in spaces. So you need to check the char after and before zero if it is equal to space. Also you need to handle the first zero and the last zero on the line separately.

sed '
    # replace 0 beeing the first character on the line
    s/^0\([[:space:]]\)/Not Exists\1/
    # replace zeros separated by spaces
    s/\([[:space:]]\)0\([[:space:]]\)/\1Not Exists\2/g
    # replace the last 0
    s/\([[:space:]]\)0&/\1Not Exists/ ' data.txt > out.txt

Live example at tutorialpoint.

0

Using sed:

sed 's/\<0\>/NotExist/g' file | column -t

\<...\> matches a word.

column -t display in column nicely.

4
  • This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here. Nov 14, 2018 at 12:01
  • Thank you Oliv. This is working. Can you elaborate regex for me to understand.
    – buddhima87
    Nov 14, 2018 at 12:02
  • @WiktorStribiżew I didn't see any float number input data...
    – oliv
    Nov 14, 2018 at 12:03
  • If you think what you suggest is a solution, you should refrain from posting duplicates as it has already been answered at stackoverflow.com/questions/1032023, stackoverflow.com/questions/46676657.... Just mark as a dupe. Nov 14, 2018 at 12:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.