Sed command runs with hardcoded value in regex but fails with variable in script [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite













This question already has an answer here:



  • How can I use variables in the LHS and RHS of a sed substitution?

    11 answers



My file data Pattern is below and i need output as 6 or 3 or 8 or 4 based on the variable value which is eth0 and eth1



eth0RX:6:eth0TX:3|eth1RX:8:eth1TX:4|


Below code works well



sed 's/.*eth0RX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt


But fails when I use dynamic variable like below



rxfile=sed 's/.*$iRX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt









share|improve this question















marked as duplicate by Kusalananda 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();

);
);
);
Oct 2 '17 at 5:28


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.


















    up vote
    1
    down vote

    favorite













    This question already has an answer here:



    • How can I use variables in the LHS and RHS of a sed substitution?

      11 answers



    My file data Pattern is below and i need output as 6 or 3 or 8 or 4 based on the variable value which is eth0 and eth1



    eth0RX:6:eth0TX:3|eth1RX:8:eth1TX:4|


    Below code works well



    sed 's/.*eth0RX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt


    But fails when I use dynamic variable like below



    rxfile=sed 's/.*$iRX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt









    share|improve this question















    marked as duplicate by Kusalananda 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();

    );
    );
    );
    Oct 2 '17 at 5:28


    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.
















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite












      This question already has an answer here:



      • How can I use variables in the LHS and RHS of a sed substitution?

        11 answers



      My file data Pattern is below and i need output as 6 or 3 or 8 or 4 based on the variable value which is eth0 and eth1



      eth0RX:6:eth0TX:3|eth1RX:8:eth1TX:4|


      Below code works well



      sed 's/.*eth0RX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt


      But fails when I use dynamic variable like below



      rxfile=sed 's/.*$iRX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt









      share|improve this question
















      This question already has an answer here:



      • How can I use variables in the LHS and RHS of a sed substitution?

        11 answers



      My file data Pattern is below and i need output as 6 or 3 or 8 or 4 based on the variable value which is eth0 and eth1



      eth0RX:6:eth0TX:3|eth1RX:8:eth1TX:4|


      Below code works well



      sed 's/.*eth0RX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt


      But fails when I use dynamic variable like below



      rxfile=sed 's/.*$iRX:([0-9]+).*/1/g' $EMSTATE/packetdrop.txt




      This question already has an answer here:



      • How can I use variables in the LHS and RHS of a sed substitution?

        11 answers







      linux bash shell-script shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 2 '17 at 4:57









      Scott

      6,30942348




      6,30942348










      asked Oct 2 '17 at 4:21









      Agnesh Kumar Singh

      61




      61




      marked as duplicate by Kusalananda 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();

      );
      );
      );
      Oct 2 '17 at 5:28


      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 Kusalananda 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();

      );
      );
      );
      Oct 2 '17 at 5:28


      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.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          1. Inside single quotes, everything is what it is. 
            $i is a dollar sign followed by an i. 
            If you want to use a variable,
            you have to put the string into double quotes, somewhat like this:

            "s/.*$iRX:([0-9]+).*/1/g"


          2. But that won’t work, because it will look for a variable called iRX. 
            The simplest way to fix that is to put the variable name in braces, like this:

            "s/.*$iRX:([0-9]+).*/1/g"


          3. You should put $EMSTATE into double quotes, too. 
            You can use "$EMSTATE"/packetdrop.txt or "$EMSTATE/packetdrop.txt",
            whichever you prefer.





          share|improve this answer




















          • Yes, there is a significant overlap.   (My answer is not the same as yours, or a subset, as you didn’t mention my point #3.)   But, more importantly, I started typing my answer before you posted yours.  And I believe that my explanation is somewhat clearer than yours.
            – Scott
            Oct 2 '17 at 6:16

















          up vote
          2
          down vote













          Variable expansion doesn't happen in single quoting. Use double quote instead. And if you want to set result to a variable, you need to run sed within command substitution syntax $(...) and better to quote this as well "$(...)". Plus as $iRX can be a valid variable name better do $i.



          rxfile="$(sed "s/.*$iRX:([0-9]+).*/1/g" "$EMSTATE/packetdrop.txt" )"





          share|improve this answer





























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            1. Inside single quotes, everything is what it is. 
              $i is a dollar sign followed by an i. 
              If you want to use a variable,
              you have to put the string into double quotes, somewhat like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            2. But that won’t work, because it will look for a variable called iRX. 
              The simplest way to fix that is to put the variable name in braces, like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            3. You should put $EMSTATE into double quotes, too. 
              You can use "$EMSTATE"/packetdrop.txt or "$EMSTATE/packetdrop.txt",
              whichever you prefer.





            share|improve this answer




















            • Yes, there is a significant overlap.   (My answer is not the same as yours, or a subset, as you didn’t mention my point #3.)   But, more importantly, I started typing my answer before you posted yours.  And I believe that my explanation is somewhat clearer than yours.
              – Scott
              Oct 2 '17 at 6:16














            up vote
            2
            down vote













            1. Inside single quotes, everything is what it is. 
              $i is a dollar sign followed by an i. 
              If you want to use a variable,
              you have to put the string into double quotes, somewhat like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            2. But that won’t work, because it will look for a variable called iRX. 
              The simplest way to fix that is to put the variable name in braces, like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            3. You should put $EMSTATE into double quotes, too. 
              You can use "$EMSTATE"/packetdrop.txt or "$EMSTATE/packetdrop.txt",
              whichever you prefer.





            share|improve this answer




















            • Yes, there is a significant overlap.   (My answer is not the same as yours, or a subset, as you didn’t mention my point #3.)   But, more importantly, I started typing my answer before you posted yours.  And I believe that my explanation is somewhat clearer than yours.
              – Scott
              Oct 2 '17 at 6:16












            up vote
            2
            down vote










            up vote
            2
            down vote









            1. Inside single quotes, everything is what it is. 
              $i is a dollar sign followed by an i. 
              If you want to use a variable,
              you have to put the string into double quotes, somewhat like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            2. But that won’t work, because it will look for a variable called iRX. 
              The simplest way to fix that is to put the variable name in braces, like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            3. You should put $EMSTATE into double quotes, too. 
              You can use "$EMSTATE"/packetdrop.txt or "$EMSTATE/packetdrop.txt",
              whichever you prefer.





            share|improve this answer












            1. Inside single quotes, everything is what it is. 
              $i is a dollar sign followed by an i. 
              If you want to use a variable,
              you have to put the string into double quotes, somewhat like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            2. But that won’t work, because it will look for a variable called iRX. 
              The simplest way to fix that is to put the variable name in braces, like this:

              "s/.*$iRX:([0-9]+).*/1/g"


            3. You should put $EMSTATE into double quotes, too. 
              You can use "$EMSTATE"/packetdrop.txt or "$EMSTATE/packetdrop.txt",
              whichever you prefer.






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 2 '17 at 5:05









            Scott

            6,30942348




            6,30942348











            • Yes, there is a significant overlap.   (My answer is not the same as yours, or a subset, as you didn’t mention my point #3.)   But, more importantly, I started typing my answer before you posted yours.  And I believe that my explanation is somewhat clearer than yours.
              – Scott
              Oct 2 '17 at 6:16
















            • Yes, there is a significant overlap.   (My answer is not the same as yours, or a subset, as you didn’t mention my point #3.)   But, more importantly, I started typing my answer before you posted yours.  And I believe that my explanation is somewhat clearer than yours.
              – Scott
              Oct 2 '17 at 6:16















            Yes, there is a significant overlap.   (My answer is not the same as yours, or a subset, as you didn’t mention my point #3.)   But, more importantly, I started typing my answer before you posted yours.  And I believe that my explanation is somewhat clearer than yours.
            – Scott
            Oct 2 '17 at 6:16




            Yes, there is a significant overlap.   (My answer is not the same as yours, or a subset, as you didn’t mention my point #3.)   But, more importantly, I started typing my answer before you posted yours.  And I believe that my explanation is somewhat clearer than yours.
            – Scott
            Oct 2 '17 at 6:16












            up vote
            2
            down vote













            Variable expansion doesn't happen in single quoting. Use double quote instead. And if you want to set result to a variable, you need to run sed within command substitution syntax $(...) and better to quote this as well "$(...)". Plus as $iRX can be a valid variable name better do $i.



            rxfile="$(sed "s/.*$iRX:([0-9]+).*/1/g" "$EMSTATE/packetdrop.txt" )"





            share|improve this answer


























              up vote
              2
              down vote













              Variable expansion doesn't happen in single quoting. Use double quote instead. And if you want to set result to a variable, you need to run sed within command substitution syntax $(...) and better to quote this as well "$(...)". Plus as $iRX can be a valid variable name better do $i.



              rxfile="$(sed "s/.*$iRX:([0-9]+).*/1/g" "$EMSTATE/packetdrop.txt" )"





              share|improve this answer
























                up vote
                2
                down vote










                up vote
                2
                down vote









                Variable expansion doesn't happen in single quoting. Use double quote instead. And if you want to set result to a variable, you need to run sed within command substitution syntax $(...) and better to quote this as well "$(...)". Plus as $iRX can be a valid variable name better do $i.



                rxfile="$(sed "s/.*$iRX:([0-9]+).*/1/g" "$EMSTATE/packetdrop.txt" )"





                share|improve this answer














                Variable expansion doesn't happen in single quoting. Use double quote instead. And if you want to set result to a variable, you need to run sed within command substitution syntax $(...) and better to quote this as well "$(...)". Plus as $iRX can be a valid variable name better do $i.



                rxfile="$(sed "s/.*$iRX:([0-9]+).*/1/g" "$EMSTATE/packetdrop.txt" )"






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 2 '17 at 5:11

























                answered Oct 2 '17 at 4:59









                αғsнιη

                15.7k92563




                15.7k92563












                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?