Command to extract value between two variables and store it in a variable [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash 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







share|improve this 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














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







share|improve this 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












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







share|improve this question











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









share|improve this question










share|improve this question




share|improve this question









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












  • 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










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




  1. 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'



  2. 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'
    hello60world



  3. also 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






share|improve this answer




























    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




    1. 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'



    2. 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'
      hello60world



    3. also 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






    share|improve this answer

























      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




      1. 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'



      2. 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'
        hello60world



      3. also 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






      share|improve this answer























        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




        1. 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'



        2. 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'
          hello60world



        3. also 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






        share|improve this answer













        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




        1. 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'



        2. 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'
          hello60world



        3. also 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







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 28 at 14:24









        glenn jackman

        45.7k265100




        45.7k265100












            Popular posts from this blog

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay