0

I need to extract a number from the output of a command: cmd. The output is type: 1000 So my question is how to execute the command, store its output in a variable and extract 1000 in a shell script. Also how do you store the extracted string in a variable?

Etheryte
  • 24,589
  • 11
  • 71
  • 116
J Freebird
  • 3,664
  • 7
  • 46
  • 81

6 Answers6

1

You tagged your question as sed but your question description does not restrict other tools, so here's a solution using awk.

output = `cmd | awk -F':' '/type: [0-9]+/{print $2}'`

Alternatively, you can use the newer $( ) syntax. Some find the newer syntax preferable and it can be conveniently nested, without the need for escaping backtics.

output = $(cmd | awk -F':' '/type: [0-9]+/{print $2}')
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • type: 1000 is only one of the many lines from the output. So I think I want to match the exact pattern, and then extract the string following it. Thanks. – J Freebird Jun 30 '14 at 23:35
  • Why not using just using `awk`? `cmd | awk '/type : [0-9]+/ {print $3;exit}`. `grep` and especially `egrep` isn't required. However, I would still use `sed` – hek2mgl Jun 30 '14 at 23:47
  • You should not use old and outdated back ticks, use parentheses `$(code)` like this `output=$(cmd | awk -F':' '/type: [0-9]+/{print $2}')` – Jotne Jul 01 '14 at 04:47
  • @jotne the op did not mention which shell he is using and I believe the old style is more portable across shells. Please correct if I'm mistaken. – merlin2011 Jul 01 '14 at 05:16
  • As far as I have found and what other has written, you need a very old and obscure system to not have parentheses. If you are in doubt, post both and say that its preferable to use parentheses. (do a google search on it) – Jotne Jul 01 '14 at 05:21
  • @Jotne, I have updated it to include both, but note that the link inside the accepted answer [here](http://stackoverflow.com/a/4708569/391161) does not say one is deprecated in favor of the other. – merlin2011 Jul 01 '14 at 05:40
  • The syntax in this answer is wrong; you can't have whitespace on either side of the `=` in a shell script variabde assignment. – tripleee Jan 16 '22 at 12:35
1

This is a typical use case for grep:

output=$(cmd | grep -o '[0-9]\+')

You can write the output of a command or even a pipeline of commands into a shell variable using so called command substitution:

variable=$(cmd);

In comments it appeared that the output of cmd contains more lines than the type : 1000. In this case I would suggest sed:

output=$(cmd | sed -n 's/type : \([0-9]\+\)/\1/p;q')
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • type: 1000 is only one of the many lines from the output. So I think I want to match the exact patter, and then extract the string following it. Thanks. – J Freebird Jun 30 '14 at 23:33
  • ah, ok.. then I would use `sed`... gimme a sec. Btw, you see that nobody got this detail. You should enhance your question. – hek2mgl Jun 30 '14 at 23:33
  • right, I got to know how to do it with cut. Thanks a lot anyway – J Freebird Jun 30 '14 at 23:41
  • ??? sorry, this makes `0` sense! how could cut handle mutltiple lines in output? Your are accepting and upvoting actually the worst answers – hek2mgl Jun 30 '14 at 23:43
1

This question has been answered in pieces here before, it would be something like this:

line=$(sed -n '2p' myfile)
echo "$line"


if [ `echo $line || grep 'type: 1000' ` ] then;
  echo "It's there!";
fi;

Store output of sed into a variable

String contains in Bash

EDIT: sed is very limited, you would need to use bash, perl or awk for what you need.

Community
  • 1
  • 1
Jesse Hernandez
  • 307
  • 2
  • 15
0

You can use the | operator to send the output of one command to another, like so:

echo " 1\n 2\n 3\n" | grep "2"

This sends the string " 1\n 2\n 3\n" to the grep command, which will search for the line containing 2. It sound like you might want to do something like:

cmd | grep "type"
Jeremy Sigrist
  • 325
  • 2
  • 11
0

If the output is rigidly restricted to "type: " followed by a number, you can just use cut.

var=$(echo 'type: 1000' | cut -f 2 -d ' ')

Obviously you'll have to pipe the output of your command to cut, I'm using echo as a demo.

In addition, I'd use grep and then cut if the string you are searching is more complex. If we assume there can be all kind of numbers in the text, but only one occurrence of "type: " followed by a number, you can use the command:

>> var=$(echo "hello 12 type: 1000 foo 1001" | grep -oE "type: [0-9]+" | cut -f 2 -d ' ')
>> echo $var
1000
timgeb
  • 76,762
  • 20
  • 123
  • 145
0

Here is a plain sed solution that uses a regualar expression to find the number in your string:

cmd | sed 's/^.*type: \([0-9]\+\)/\1/g'
  • ^ means from the start
  • .* can be any character (also none)
  • \([0-9]\+\) are numbers (minimum one character)
  • \1 means it takes the first pattern it finds (and only in this case) and uses it as replacement for the whole string
rubo77
  • 19,527
  • 31
  • 134
  • 226