Learner_51
Learner_51

Reputation: 1075

Unix cut operation

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

Answers (6)

Dennis Williamson
Dennis Williamson

Reputation: 359965

var1=$( echo "uid=2560(jdihenia) gid=1000(undergrad)" | grep -Po 'gid=.*\(\K.*(?=\))')

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246764

echo "$str" | awk -F'[()]' '{print $4}'

Upvotes: 2

jilles
jilles

Reputation: 11232

var1="uid=2560(jdihenia) gid=1000(undergrad)"
var1=${var1#*\(*\(}
var1=${var1%%\)*}

Upvotes: 2

wilhelmtell
wilhelmtell

Reputation: 58667

var1=$(cmd |sed 's/.*(\([^)]*\))/\1/')

Upvotes: 3

Jon
Jon

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

jilles
jilles

Reputation: 11232

If this string comes from id, then you can just call id -gn instead.

Upvotes: 3

Related Questions