Extracting the second word from a string variable
118,401
Solution 1
Try this way:
y=$(echo $x | awk '{print $2}')
echo $y
echo $x
display the value ofx
.awk '{print $2}'
prints the second field of the previously displayedx
.$(
...)
hold the output and let assign it toy
.
Solution 2
Assuming that there's at least one space before the substring you wish to extract (and that the substring does not contain any spaces), you can do this with a simple parameter expansion:
x="rtcpOnNbActive true"
y="${x##* }"
echo "[$y]"
output
[true]
Solution 3
It's possible to use bash arrays for that, just place your str
inside parenthesis. e.g.:
arr=("first second third")
echo ${arr[1]}
str="first second third"
arr1=($str)
echo ${arr1[1]}
Solution 4
you can use awk:
echo "rtcpOnNbActive true" | awk '{print $NF}'
true
NF
number of field in the current record
using sed:
echo "rtcpOnNbActive true" | sed 's/.* //g'
true
using string expression:
a="rtcpOnNbActive true"
echo ${a##* }
true
using grep:
echo "rtcpOnNbActive true" | grep -Eo "[a-z]+$"
true
-o is gives only exact match, [a-z]+
will match letter from a-z and $
means at end
Solution 5
You could use the read
built-in
read -r _ y <<<"$x"
printf "%s\n" "$y"
true
Related videos on Youtube
Author by
Pratibha Jain
Updated on September 18, 2022Comments
-
Pratibha Jain over 1 year
I have a string
"rtcpOnNbActive true"
stored in a variablex
. I want to extract "true" as substring and store in a variable. How can I do this?-
PM 2Ring over 9 yearsWill there always be a space in
x
just before the substring you want to extract?
-
-
Stéphane Chazelas over 9 years
echo $x
is not display the value ofx
.printf '%s\n' "$x"
would be. -
Stéphane Chazelas over 9 years
awk '{print $2}'
prints the second field of each line of the previously displayedx
. -
αғsнιη over 9 yearsDon't post content of other answer for yours in the same question, please.
-
nyuszika7h over 9 years
echo
is not portable for anything other than a literal string that doesn't start with-
and doesn't contain any escape sequences. Its behavior varies even for the bash builtin, depending on how it was compiled, and whetherXPG_ECHO
is set. Assuming the string contains no escape sequences, this should be fine, butprintf '[%s]\n' "$y"
is still better. -
nyuszika7h over 9 years
echo
is not portable for anything other than a literal string that doesn't start with-
and doesn't contain any escape sequences. Its behavior varies even for the bash builtin, depending on how it was compiled, and whetherXPG_ECHO
is set. Also, you should always double quote variable expansions and command substitution (with certain exceptions, where it's not necessary but it doesn't do any harm either). With a string like the OP's, this should be fine, but if you want to make sureprintf '%s\n' "${a##* }"
would be better. -
mikeserv over 9 yearswhy involve
read
? It isn'tread
that does the split - it is$IFS
. For some reason a lot of people consider it ok to split w/$IFS
only whenread
is involved. You can just do:set -f; IFS=' '; printf %.0s%s $x
or whatever. In any case - you need to specify$IFS
's value here. -
Mathias Begert over 9 years@mikeserv, it's
read
doing the splitting usingIFS
, check the documentation. The main advantage with using read is that it sets variables (which is an OP requirement) and given that it can split strings and populate an array, is good for extracting arbitrary fields from a string. Additionally, I am assuming defaultIFS
here, but it's easy enough to set if needed,IFS=$' \n\t' read -r _ y <<<"$x"
will do the trick -
Mathias Begert over 9 years@mikeserv, you're off my Christmas card list.
-
PM 2Ring over 9 years@nyuszika7h: Good point, and after having read Stéphane Chazelas say similar things here and in other recent questions I really ought to break my habit of using
echo
to display the value of variables, even in "throw-away" examples like this. -
mikeserv over 9 yearsbah humbug. xmas is for suckers.
-
prashant thakre about 3 yearsThis is the best approach thanks its very simple and not an errorpron.
-
Cecil Curry about 2 yearsThe only sane solution. Every other solution either invokes external third-party commands that may not necessarily be available or behave as expected under the current platform (e.g.,
awk
) or performs fragile string-munging guaranteed to fail in obvious corner cases (e.g.,echo "${x##* }"
).