137

I have a string that looks like this:

 GenFiltEff=7.092200e-01

Using bash, I would like to just get the number after the = character. Is there a way to do this?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
user788171
  • 16,753
  • 40
  • 98
  • 125
  • 4
    This should be reopened. `How to grep` is asking how you do it with grep, this question is asking how to get string after character... not tied with `grep`. – Jamie Hutber Dec 17 '18 at 12:19

5 Answers5

168

Use parameter expansion, if the value is already stored in a variable.

$ str="GenFiltEff=7.092200e-01"
$ value=${str#*=}

Or use read

$ IFS="=" read name value <<< "GenFiltEff=7.092200e-01"

Either way,

$ echo $value
7.092200e-01
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 45
    If the string has more than one "=": `${str##*=} to get from the last match. ${str#*=} to get from the first match. ${str%%=*} to get until the first match. ${str%=*} to get until the last match.` [String Manipulation @ tldp.org] (http://tldp.org/LDP/abs/html/string-manipulation.html) – lepe Dec 27 '14 at 04:36
  • I didnt get the second solution to work: `$ IFS="=" read name value <<< "GenFiltEff=7.092200e-01"` using bash 4.4.5 – sn1ckers Jan 24 '17 at 13:39
  • Works fine for me in 4.4.5. If you cannot figure out the problem, though, please open a new question that includes the result you *do* get. – chepner Jan 24 '17 at 13:52
  • 2
    It would be great if this answer explained itself in more detail. What exactly do each of `#*=` do here? – buildsucceeded Feb 15 '17 at 17:09
  • 1
    The `#` operator omits the shortest prefix that matches the pattern `*=` from the expansion of `$str`. – chepner Feb 15 '17 at 17:39
134

For the text after the first = and before the next =

cut -d "=" -f2 <<< "$your_str"

or

sed -e 's#.*=\(\)#\1#' <<< "$your_str"

For all text after the first = regardless of if there are multiple =

cut -d "=" -f2- <<< "$your_str"
Elle H
  • 11,837
  • 7
  • 39
  • 42
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • 11
    Good for simple cases, however these methods don't play well when there is more than one "=" in the string. Chepner's first solution is simple and more reliable. – lepe Dec 27 '14 at 04:40
  • The answer is more useful when using | to run commands in a chain then when using variables. – Mohammad Yasir K P Jun 13 '22 at 11:32
25
echo "GenFiltEff=7.092200e-01" | cut -d "=" -f2 
Greg Guida
  • 7,302
  • 4
  • 30
  • 40
6

This should work:

your_str='GenFiltEff=7.092200e-01'
echo $your_str | cut -d "=" -f2
jman
  • 11,334
  • 5
  • 39
  • 61
5
${word:$(expr index "$word" "="):1}

that gets the 7. Assuming you mean the entire rest of the string, just leave off the :1.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • `$value = ${word:$(expr index "$word" "=")}` worked well for me to return everything after the equal sign. – DTrejo Apr 30 '19 at 21:55