I'm looking to parse the output of the id command.
uid=205(oracle) gid=203(dba) groups=4(adm),207(oinstall),202(ndm),206(eis)
Is there an easy way I can get the user name (in this case "oracle) using a sed or awk command. The username will always be inside the parenthsis
and proceeded with uid=<some number>
Any help would be greatly appreciated.
Thanks in advance to all who answer
You could try something along the following lines (I don't know how portable it is, you should check the man pages on your system):
id -un
ctsgnb
October 12, 2010, 3:05pm
3
Can't you use the $LOGNAME shell build-in variable ?
echo $LOGNAME
It's not portable amongst different flavors of UNIX that's why I dont
want to use it. Thanks for responding.
Ie: on Solaris:
id -un
id: illegal option -- u
You could try the POSIX version:
/usr/xpg4/bin/id -un
$ uname -sr
SunOS 5.8
$ id -un
id: illegal option -- u
Usage: id [-ap] [user]
$ PATH="$(getconf PATH)" id -un
drado
The username must be the first parenthetical:
awk -F'[()]' '{print $2}'
A smarter approach:
sed 's/^.*uid=[0-9]*(\([^)]*\).*/\1/'
Regards,
Alister
birei
October 13, 2010, 4:19pm
7
Hi,
Another 'sed' solution:
id | sed 's/[^(]\+(\([^)]\+\)).*/\1/'
Regards,
Birei
To be specific, a gnu sed solution. \+ is an extension to the posix sed standard.
Regards,
Alister