1
Let's say I'm doing a grep
and it returns this line:
Invalid value (48) on line 3
How can I easily pull that value 48
into a variable in Bourne shell?
1
Let's say I'm doing a grep
and it returns this line:
Invalid value (48) on line 3
How can I easily pull that value 48
into a variable in Bourne shell?
1
If you are sure that the pattern is always to get the value in the first pair of parenthesis, then cut is your best friend.
myvar=$(echo 'Invalid value (48) on line 3' | cut -d\( -f2 | cut -d\) -f1)
this extracts the value between the parens.
$()
is not part of SVID and thus not strictly "Bourne Shell" only. It's defined in POSIX though. – slhck – 2013-08-13T18:13:23.360
yes, if it is indeed the original sh, though in most linux distributions /bin/sh is just a symlink to /bin/bash. however, backstick should work, if $() does not. just remove $ and replace () with ``. – johnshen64 – 2013-08-13T18:16:42.680
Yup, just wanted to mention it for completeness since the OP seemed to be very specific about sh
and not Bash and companions. – slhck – 2013-08-13T18:18:56.923
0
echo 'Invalid value (48) on line 3'| awk -F'[()]' '{print $2}'
With pure "classic" Bourne Shell commands, or can you use external tools? No Bash or anything recent? – slhck – 2013-08-13T16:35:50.543