sed find and replace a specific number [duplicate]










1
















This question already has an answer here:



  • sed whole word search and replace

    6 answers



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.










share|improve this question















marked as duplicate by Wiktor Stribiżew, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 5:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    1
















    This question already has an answer here:



    • sed whole word search and replace

      6 answers



    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.










    share|improve this question















    marked as duplicate by Wiktor Stribiżew, tripleee bash
    Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Nov 15 '18 at 5:40


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      1












      1








      1









      This question already has an answer here:



      • sed whole word search and replace

        6 answers



      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.










      share|improve this question

















      This question already has an answer here:



      • sed whole word search and replace

        6 answers



      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.





      This question already has an answer here:



      • sed whole word search and replace

        6 answers







      bash shell awk sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 13:40









      RavinderSingh13

      27.3k41538




      27.3k41538










      asked Nov 14 '18 at 11:49









      buddhima87buddhima87

      115




      115




      marked as duplicate by Wiktor Stribiżew, tripleee bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 5:40


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Wiktor Stribiżew, tripleee bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Nov 15 '18 at 5:40


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          4 Answers
          4






          active

          oldest

          votes


















          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.





          share|improve this answer

























          • Thank you Ravinder. This is what what I expected.

            – buddhima87
            Nov 14 '18 at 12:03











          • Can you please let me know what last 1 stands for ?

            – buddhima87
            Nov 14 '18 at 12:06











          • @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers

            – RavinderSingh13
            Nov 14 '18 at 12:06







          • 1





            Dang. We typed the same awk code, almost exactly. :)

            – ghoti
            Nov 14 '18 at 12:21






          • 1





            Thank Ravinder. You explained it well.

            – buddhima87
            Nov 14 '18 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 exist2/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 exist2/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.






          share|improve this answer

























          • Thank you @ghoti. This helped me to shape up my final output.

            – buddhima87
            Jan 2 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 Exists1/
          # replace zeros separated by spaces
          s/([[:space:]])0([[:space:]])/1Not Exists2/g
          # replace the last 0
          s/([[:space:]])0&/1Not Exists/ ' data.txt > out.txt


          Live example at tutorialpoint.






          share|improve this answer






























            0














            Using sed:



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


            <...> matches a word.



            column -t display in column nicely.






            share|improve this answer























            • This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here.

              – Wiktor Stribiżew
              Nov 14 '18 at 12:01












            • Thank you Oliv. This is working. Can you elaborate regex for me to understand.

              – buddhima87
              Nov 14 '18 at 12:02











            • @WiktorStribiżew I didn't see any float number input data...

              – oliv
              Nov 14 '18 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.

              – Wiktor Stribiżew
              Nov 14 '18 at 12:07


















            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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.





            share|improve this answer

























            • Thank you Ravinder. This is what what I expected.

              – buddhima87
              Nov 14 '18 at 12:03











            • Can you please let me know what last 1 stands for ?

              – buddhima87
              Nov 14 '18 at 12:06











            • @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers

              – RavinderSingh13
              Nov 14 '18 at 12:06







            • 1





              Dang. We typed the same awk code, almost exactly. :)

              – ghoti
              Nov 14 '18 at 12:21






            • 1





              Thank Ravinder. You explained it well.

              – buddhima87
              Nov 14 '18 at 12:48















            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.





            share|improve this answer

























            • Thank you Ravinder. This is what what I expected.

              – buddhima87
              Nov 14 '18 at 12:03











            • Can you please let me know what last 1 stands for ?

              – buddhima87
              Nov 14 '18 at 12:06











            • @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers

              – RavinderSingh13
              Nov 14 '18 at 12:06







            • 1





              Dang. We typed the same awk code, almost exactly. :)

              – ghoti
              Nov 14 '18 at 12:21






            • 1





              Thank Ravinder. You explained it well.

              – buddhima87
              Nov 14 '18 at 12:48













            3












            3








            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.





            share|improve this answer















            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.






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 12:14

























            answered Nov 14 '18 at 11:54









            RavinderSingh13RavinderSingh13

            27.3k41538




            27.3k41538












            • Thank you Ravinder. This is what what I expected.

              – buddhima87
              Nov 14 '18 at 12:03











            • Can you please let me know what last 1 stands for ?

              – buddhima87
              Nov 14 '18 at 12:06











            • @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers

              – RavinderSingh13
              Nov 14 '18 at 12:06







            • 1





              Dang. We typed the same awk code, almost exactly. :)

              – ghoti
              Nov 14 '18 at 12:21






            • 1





              Thank Ravinder. You explained it well.

              – buddhima87
              Nov 14 '18 at 12:48

















            • Thank you Ravinder. This is what what I expected.

              – buddhima87
              Nov 14 '18 at 12:03











            • Can you please let me know what last 1 stands for ?

              – buddhima87
              Nov 14 '18 at 12:06











            • @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers

              – RavinderSingh13
              Nov 14 '18 at 12:06







            • 1





              Dang. We typed the same awk code, almost exactly. :)

              – ghoti
              Nov 14 '18 at 12:21






            • 1





              Thank Ravinder. You explained it well.

              – buddhima87
              Nov 14 '18 at 12:48
















            Thank you Ravinder. This is what what I expected.

            – buddhima87
            Nov 14 '18 at 12:03





            Thank you Ravinder. This is what what I expected.

            – buddhima87
            Nov 14 '18 at 12:03













            Can you please let me know what last 1 stands for ?

            – buddhima87
            Nov 14 '18 at 12:06





            Can you please let me know what last 1 stands for ?

            – buddhima87
            Nov 14 '18 at 12:06













            @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers

            – RavinderSingh13
            Nov 14 '18 at 12:06






            @buddhima87, glad that it helped you, try to up-vote helpful answers. Try to select an answer as correct too out of all see this stackoverflow.com/help/someone-answers

            – RavinderSingh13
            Nov 14 '18 at 12:06





            1




            1





            Dang. We typed the same awk code, almost exactly. :)

            – ghoti
            Nov 14 '18 at 12:21





            Dang. We typed the same awk code, almost exactly. :)

            – ghoti
            Nov 14 '18 at 12:21




            1




            1





            Thank Ravinder. You explained it well.

            – buddhima87
            Nov 14 '18 at 12:48





            Thank Ravinder. You explained it well.

            – buddhima87
            Nov 14 '18 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 exist2/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 exist2/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.






            share|improve this answer

























            • Thank you @ghoti. This helped me to shape up my final output.

              – buddhima87
              Jan 2 at 16:15















            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 exist2/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 exist2/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.






            share|improve this answer

























            • Thank you @ghoti. This helped me to shape up my final output.

              – buddhima87
              Jan 2 at 16:15













            2












            2








            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 exist2/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 exist2/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.






            share|improve this answer















            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 exist2/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 exist2/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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 12:22

























            answered Nov 14 '18 at 12:12









            ghotighoti

            35.2k74279




            35.2k74279












            • Thank you @ghoti. This helped me to shape up my final output.

              – buddhima87
              Jan 2 at 16:15

















            • Thank you @ghoti. This helped me to shape up my final output.

              – buddhima87
              Jan 2 at 16:15
















            Thank you @ghoti. This helped me to shape up my final output.

            – buddhima87
            Jan 2 at 16:15





            Thank you @ghoti. This helped me to shape up my final output.

            – buddhima87
            Jan 2 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 Exists1/
            # replace zeros separated by spaces
            s/([[:space:]])0([[:space:]])/1Not Exists2/g
            # replace the last 0
            s/([[:space:]])0&/1Not Exists/ ' data.txt > out.txt


            Live example at tutorialpoint.






            share|improve this answer



























              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 Exists1/
              # replace zeros separated by spaces
              s/([[:space:]])0([[:space:]])/1Not Exists2/g
              # replace the last 0
              s/([[:space:]])0&/1Not Exists/ ' data.txt > out.txt


              Live example at tutorialpoint.






              share|improve this answer

























                1












                1








                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 Exists1/
                # replace zeros separated by spaces
                s/([[:space:]])0([[:space:]])/1Not Exists2/g
                # replace the last 0
                s/([[:space:]])0&/1Not Exists/ ' data.txt > out.txt


                Live example at tutorialpoint.






                share|improve this answer













                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 Exists1/
                # replace zeros separated by spaces
                s/([[:space:]])0([[:space:]])/1Not Exists2/g
                # replace the last 0
                s/([[:space:]])0&/1Not Exists/ ' data.txt > out.txt


                Live example at tutorialpoint.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 11:56









                Kamil CukKamil Cuk

                10.4k1527




                10.4k1527





















                    0














                    Using sed:



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


                    <...> matches a word.



                    column -t display in column nicely.






                    share|improve this answer























                    • This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:01












                    • Thank you Oliv. This is working. Can you elaborate regex for me to understand.

                      – buddhima87
                      Nov 14 '18 at 12:02











                    • @WiktorStribiżew I didn't see any float number input data...

                      – oliv
                      Nov 14 '18 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.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:07
















                    0














                    Using sed:



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


                    <...> matches a word.



                    column -t display in column nicely.






                    share|improve this answer























                    • This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:01












                    • Thank you Oliv. This is working. Can you elaborate regex for me to understand.

                      – buddhima87
                      Nov 14 '18 at 12:02











                    • @WiktorStribiżew I didn't see any float number input data...

                      – oliv
                      Nov 14 '18 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.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:07














                    0












                    0








                    0







                    Using sed:



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


                    <...> matches a word.



                    column -t display in column nicely.






                    share|improve this answer













                    Using sed:



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


                    <...> matches a word.



                    column -t display in column nicely.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 11:57









                    olivoliv

                    8,3811130




                    8,3811130












                    • This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:01












                    • Thank you Oliv. This is working. Can you elaborate regex for me to understand.

                      – buddhima87
                      Nov 14 '18 at 12:02











                    • @WiktorStribiżew I didn't see any float number input data...

                      – oliv
                      Nov 14 '18 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.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:07


















                    • This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:01












                    • Thank you Oliv. This is working. Can you elaborate regex for me to understand.

                      – buddhima87
                      Nov 14 '18 at 12:02











                    • @WiktorStribiżew I didn't see any float number input data...

                      – oliv
                      Nov 14 '18 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.

                      – Wiktor Stribiżew
                      Nov 14 '18 at 12:07

















                    This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here.

                    – Wiktor Stribiżew
                    Nov 14 '18 at 12:01






                    This will also match 0 in 0.234 (if any). Whitespaces are the boundaries here.

                    – Wiktor Stribiżew
                    Nov 14 '18 at 12:01














                    Thank you Oliv. This is working. Can you elaborate regex for me to understand.

                    – buddhima87
                    Nov 14 '18 at 12:02





                    Thank you Oliv. This is working. Can you elaborate regex for me to understand.

                    – buddhima87
                    Nov 14 '18 at 12:02













                    @WiktorStribiżew I didn't see any float number input data...

                    – oliv
                    Nov 14 '18 at 12:03






                    @WiktorStribiżew I didn't see any float number input data...

                    – oliv
                    Nov 14 '18 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.

                    – Wiktor Stribiżew
                    Nov 14 '18 at 12:07






                    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.

                    – Wiktor Stribiżew
                    Nov 14 '18 at 12:07




                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線