Reputation: 1075
I have a string like this:
uid=2560(jdihenia) gid=1000(undergrad)
I want to just get the undergrad
part in to a variable name var1. So I used a command
var1=`echo "uid=2560(jdihenia) gid=1000(undergrad)" | cut -d "(" -f 3`
but this will assign the value undergrad)
in to var1. Can you please tell me how can I get just the undergrad
part in to the variable var1?
Upvotes: 3
Views: 377
Reputation: 359965
var1=$( echo "uid=2560(jdihenia) gid=1000(undergrad)" | grep -Po 'gid=.*\(\K.*(?=\))')
Upvotes: 1
Reputation: 11232
var1="uid=2560(jdihenia) gid=1000(undergrad)"
var1=${var1#*\(*\(}
var1=${var1%%\)*}
Upvotes: 2
Reputation: 16728
If you want the literal text "undergrad" in the brackets, this should work:
cut -d "(" -f 2 <text> | cut -d ")" -f 1
or equivalently
echo <text> | cut -d "(" -f 2 | cut -d ")" -f 1
Upvotes: 3
Reputation: 11232
If this string comes from id
, then you can just call id -gn
instead.
Upvotes: 3