i have a string like
a=b c=d e=f g=h i=j j=k
How to parse this string to get the value of j?
i have a string like
a=b c=d e=f g=h i=j j=k
How to parse this string to get the value of j?
This will print anything followed by j= till upcoming space regardless of column number of j.
echo "a=b c=d e=f g=h i=j j=k" |grep -oP 'j=\K[^ ]+'
k
If you mean, the value of last column, which in this case is j=k:
echo "a=b c=d e=f g=h i=j j=k" |awk '{split($NF,a,"=");print a[2]}'
k
Note: Solution 1 is based on regex for the value of j, whereas solution 2 is for the last column's value. Its up to you to chose which one fits better to your requirement.
Or using gensub of awk:
echo "a=b c=d e=f g=h i=j j=k" |awk '{J=gensub(/.*j=([^ ]+).*/,"\\1","g");print J}'
k
or using gawk match function:
echo "a=b c=d e=f g=h i=j j=k" |awk '{match($0,/.*j=([^ ]+).*/,a);print a[1]}'
k
or using sed back refrencing:
echo "a=b c=d e=f g=h i=j j=k"|sed -r 's/.*j=([^ ]+).*/\1/'
k
With awk:
awk '{ for (i=1;i<=NF;i++) { split($i,arr,"=");if (arr[1]=="j") { print arr[2] } } }' <<< "a=b c=d e=f g=h i=j j=k"
Look through each space separated field and then further split the fields into an array (arr) delimited on =. If the first subscript of the array is j, print the second subscript.