Command to extract value between two variables and store it in a variable [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I am trying to extract the value(number) between two variables and store the value in another variable.
I am using sed command, but I am not getting any output.
#!/usr/bin/env bash
variable1=`cat $1`
variable2=`echo "$variable1" | cut -c 18`
echo "$variable2"
variable3=`echo "$variable1" | cut -c 60`
echo "$variable3"
string="$variable3xyz$variable2"
echo $string
line=`sed -n '/'"$string"'/,/'"$variable2"'/ p' $1`
echo $line
Please suggest me a way to extract number between $searchString and $variable2, lets say between $string which is : &xyz! and $variable2 which is : ! , I have 60 ie &xyz!60!
So I need to extract 60.
I also tried with
line=`sed -nr "s/$string([0-9]+),$variable2/1/ p" $1`
but when I echoed $line it printed the entire file content
Thanks in advance
shell-script
closed as unclear what you're asking by ilkkachu, Romeo Ninov, G-Man, Isaac, Alexander May 29 at 7:18
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
0
down vote
favorite
I am trying to extract the value(number) between two variables and store the value in another variable.
I am using sed command, but I am not getting any output.
#!/usr/bin/env bash
variable1=`cat $1`
variable2=`echo "$variable1" | cut -c 18`
echo "$variable2"
variable3=`echo "$variable1" | cut -c 60`
echo "$variable3"
string="$variable3xyz$variable2"
echo $string
line=`sed -n '/'"$string"'/,/'"$variable2"'/ p' $1`
echo $line
Please suggest me a way to extract number between $searchString and $variable2, lets say between $string which is : &xyz! and $variable2 which is : ! , I have 60 ie &xyz!60!
So I need to extract 60.
I also tried with
line=`sed -nr "s/$string([0-9]+),$variable2/1/ p" $1`
but when I echoed $line it printed the entire file content
Thanks in advance
shell-script
closed as unclear what you're asking by ilkkachu, Romeo Ninov, G-Man, Isaac, Alexander May 29 at 7:18
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
Please post a complete example with the contents of the file. It's hard to figure out what this is about in fact.
â Tomasz
May 28 at 13:58
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to extract the value(number) between two variables and store the value in another variable.
I am using sed command, but I am not getting any output.
#!/usr/bin/env bash
variable1=`cat $1`
variable2=`echo "$variable1" | cut -c 18`
echo "$variable2"
variable3=`echo "$variable1" | cut -c 60`
echo "$variable3"
string="$variable3xyz$variable2"
echo $string
line=`sed -n '/'"$string"'/,/'"$variable2"'/ p' $1`
echo $line
Please suggest me a way to extract number between $searchString and $variable2, lets say between $string which is : &xyz! and $variable2 which is : ! , I have 60 ie &xyz!60!
So I need to extract 60.
I also tried with
line=`sed -nr "s/$string([0-9]+),$variable2/1/ p" $1`
but when I echoed $line it printed the entire file content
Thanks in advance
shell-script
I am trying to extract the value(number) between two variables and store the value in another variable.
I am using sed command, but I am not getting any output.
#!/usr/bin/env bash
variable1=`cat $1`
variable2=`echo "$variable1" | cut -c 18`
echo "$variable2"
variable3=`echo "$variable1" | cut -c 60`
echo "$variable3"
string="$variable3xyz$variable2"
echo $string
line=`sed -n '/'"$string"'/,/'"$variable2"'/ p' $1`
echo $line
Please suggest me a way to extract number between $searchString and $variable2, lets say between $string which is : &xyz! and $variable2 which is : ! , I have 60 ie &xyz!60!
So I need to extract 60.
I also tried with
line=`sed -nr "s/$string([0-9]+),$variable2/1/ p" $1`
but when I echoed $line it printed the entire file content
Thanks in advance
shell-script
asked May 28 at 12:51
ryt
1
1
closed as unclear what you're asking by ilkkachu, Romeo Ninov, G-Man, Isaac, Alexander May 29 at 7:18
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by ilkkachu, Romeo Ninov, G-Man, Isaac, Alexander May 29 at 7:18
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
Please post a complete example with the contents of the file. It's hard to figure out what this is about in fact.
â Tomasz
May 28 at 13:58
add a comment |Â
5
Please post a complete example with the contents of the file. It's hard to figure out what this is about in fact.
â Tomasz
May 28 at 13:58
5
5
Please post a complete example with the contents of the file. It's hard to figure out what this is about in fact.
â Tomasz
May 28 at 13:58
Please post a complete example with the contents of the file. It's hard to figure out what this is about in fact.
â Tomasz
May 28 at 13:58
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
This can be done just with shell parameter substutition:
# setup
line='hello&xyz!60!world'
string='&xyz!'
variable2='!'
# now, remove from the beginning up to the first instance of "&xyz!"
tmp=$line#*$string
# $tmp now holds 60!world
# remove from the end the last "!" and all following characters
result=$tmp%$variable2*
echo $result
# => 60
It looks like your sed command is failing due to the comma
no comma in input, so nothing matches, thus nothing printed
$ set -x
$ echo "$line" | sed -nr "s/$string([0-9]+),$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+),!/1/ p'
+ echo 'hello&xyz!60!world'remove the comma, now we have a match, but the prefix and suffix remain
$ echo "$line" | sed -nr "s/$string([0-9]+)$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+)!/1/ p'
+ echo 'hello&xyz!60!world'
hello60worldalso match pre and post text
$ echo "$line" | sed -nr "s/.*$string([0-9]+)$variable2.*/1/ p"
+ sed -nr 's/.*&xyz!([0-9]+)!.*/1/ p'
+ echo 'hello&xyz!60!world'
60
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This can be done just with shell parameter substutition:
# setup
line='hello&xyz!60!world'
string='&xyz!'
variable2='!'
# now, remove from the beginning up to the first instance of "&xyz!"
tmp=$line#*$string
# $tmp now holds 60!world
# remove from the end the last "!" and all following characters
result=$tmp%$variable2*
echo $result
# => 60
It looks like your sed command is failing due to the comma
no comma in input, so nothing matches, thus nothing printed
$ set -x
$ echo "$line" | sed -nr "s/$string([0-9]+),$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+),!/1/ p'
+ echo 'hello&xyz!60!world'remove the comma, now we have a match, but the prefix and suffix remain
$ echo "$line" | sed -nr "s/$string([0-9]+)$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+)!/1/ p'
+ echo 'hello&xyz!60!world'
hello60worldalso match pre and post text
$ echo "$line" | sed -nr "s/.*$string([0-9]+)$variable2.*/1/ p"
+ sed -nr 's/.*&xyz!([0-9]+)!.*/1/ p'
+ echo 'hello&xyz!60!world'
60
add a comment |Â
up vote
1
down vote
This can be done just with shell parameter substutition:
# setup
line='hello&xyz!60!world'
string='&xyz!'
variable2='!'
# now, remove from the beginning up to the first instance of "&xyz!"
tmp=$line#*$string
# $tmp now holds 60!world
# remove from the end the last "!" and all following characters
result=$tmp%$variable2*
echo $result
# => 60
It looks like your sed command is failing due to the comma
no comma in input, so nothing matches, thus nothing printed
$ set -x
$ echo "$line" | sed -nr "s/$string([0-9]+),$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+),!/1/ p'
+ echo 'hello&xyz!60!world'remove the comma, now we have a match, but the prefix and suffix remain
$ echo "$line" | sed -nr "s/$string([0-9]+)$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+)!/1/ p'
+ echo 'hello&xyz!60!world'
hello60worldalso match pre and post text
$ echo "$line" | sed -nr "s/.*$string([0-9]+)$variable2.*/1/ p"
+ sed -nr 's/.*&xyz!([0-9]+)!.*/1/ p'
+ echo 'hello&xyz!60!world'
60
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This can be done just with shell parameter substutition:
# setup
line='hello&xyz!60!world'
string='&xyz!'
variable2='!'
# now, remove from the beginning up to the first instance of "&xyz!"
tmp=$line#*$string
# $tmp now holds 60!world
# remove from the end the last "!" and all following characters
result=$tmp%$variable2*
echo $result
# => 60
It looks like your sed command is failing due to the comma
no comma in input, so nothing matches, thus nothing printed
$ set -x
$ echo "$line" | sed -nr "s/$string([0-9]+),$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+),!/1/ p'
+ echo 'hello&xyz!60!world'remove the comma, now we have a match, but the prefix and suffix remain
$ echo "$line" | sed -nr "s/$string([0-9]+)$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+)!/1/ p'
+ echo 'hello&xyz!60!world'
hello60worldalso match pre and post text
$ echo "$line" | sed -nr "s/.*$string([0-9]+)$variable2.*/1/ p"
+ sed -nr 's/.*&xyz!([0-9]+)!.*/1/ p'
+ echo 'hello&xyz!60!world'
60
This can be done just with shell parameter substutition:
# setup
line='hello&xyz!60!world'
string='&xyz!'
variable2='!'
# now, remove from the beginning up to the first instance of "&xyz!"
tmp=$line#*$string
# $tmp now holds 60!world
# remove from the end the last "!" and all following characters
result=$tmp%$variable2*
echo $result
# => 60
It looks like your sed command is failing due to the comma
no comma in input, so nothing matches, thus nothing printed
$ set -x
$ echo "$line" | sed -nr "s/$string([0-9]+),$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+),!/1/ p'
+ echo 'hello&xyz!60!world'remove the comma, now we have a match, but the prefix and suffix remain
$ echo "$line" | sed -nr "s/$string([0-9]+)$variable2/1/ p"
+ sed -nr 's/&xyz!([0-9]+)!/1/ p'
+ echo 'hello&xyz!60!world'
hello60worldalso match pre and post text
$ echo "$line" | sed -nr "s/.*$string([0-9]+)$variable2.*/1/ p"
+ sed -nr 's/.*&xyz!([0-9]+)!.*/1/ p'
+ echo 'hello&xyz!60!world'
60
answered May 28 at 14:24
glenn jackman
45.7k265100
45.7k265100
add a comment |Â
add a comment |Â
5
Please post a complete example with the contents of the file. It's hard to figure out what this is about in fact.
â Tomasz
May 28 at 13:58