Sed command runs with hardcoded value in regex but fails with variable in script [duplicate]
Clash 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
linux bash shell-script shell
marked as duplicate by Kusalananda
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.
add a comment |Â
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
linux bash shell-script shell
marked as duplicate by Kusalananda
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.
add a comment |Â
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
linux bash shell-script shell
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
linux bash shell-script shell
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
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
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.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
- Inside single quotes, everything is what it is.ÃÂ
$i
is a dollar sign followed by ani
.ÃÂ
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"
- 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"
- You should put
$EMSTATE
into double quotes, too.ÃÂ
You can use"$EMSTATE"/packetdrop.txt
or"$EMSTATE/packetdrop.txt"
,
whichever you prefer.
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
add a comment |Â
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" )"
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
- Inside single quotes, everything is what it is.ÃÂ
$i
is a dollar sign followed by ani
.ÃÂ
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"
- 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"
- You should put
$EMSTATE
into double quotes, too.ÃÂ
You can use"$EMSTATE"/packetdrop.txt
or"$EMSTATE/packetdrop.txt"
,
whichever you prefer.
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
add a comment |Â
up vote
2
down vote
- Inside single quotes, everything is what it is.ÃÂ
$i
is a dollar sign followed by ani
.ÃÂ
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"
- 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"
- You should put
$EMSTATE
into double quotes, too.ÃÂ
You can use"$EMSTATE"/packetdrop.txt
or"$EMSTATE/packetdrop.txt"
,
whichever you prefer.
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
add a comment |Â
up vote
2
down vote
up vote
2
down vote
- Inside single quotes, everything is what it is.ÃÂ
$i
is a dollar sign followed by ani
.ÃÂ
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"
- 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"
- You should put
$EMSTATE
into double quotes, too.ÃÂ
You can use"$EMSTATE"/packetdrop.txt
or"$EMSTATE/packetdrop.txt"
,
whichever you prefer.
- Inside single quotes, everything is what it is.ÃÂ
$i
is a dollar sign followed by ani
.ÃÂ
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"
- 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"
- You should put
$EMSTATE
into double quotes, too.ÃÂ
You can use"$EMSTATE"/packetdrop.txt
or"$EMSTATE/packetdrop.txt"
,
whichever you prefer.
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
add a comment |Â
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
add a comment |Â
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" )"
add a comment |Â
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" )"
add a comment |Â
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" )"
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" )"
edited Oct 2 '17 at 5:11
answered Oct 2 '17 at 4:59
ñÃÂsýù÷
15.7k92563
15.7k92563
add a comment |Â
add a comment |Â