Parsing the id command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing the id command
# 1  
Old 10-12-2010
Parsing the id command

I'm looking to parse the output of the id command.

Code:
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

Last edited by radoulov; 10-12-2010 at 03:56 PM.. Reason: Added code tags!
# 2  
Old 10-12-2010
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):

Code:
id -un

# 3  
Old 10-12-2010
Can't you use the $LOGNAME shell build-in variable ?

echo $LOGNAME

Last edited by ctsgnb; 10-12-2010 at 04:29 PM..
# 4  
Old 10-12-2010
Quote:
Originally Posted by radoulov
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):

Code:
id -un

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
# 5  
Old 10-12-2010
You could try the POSIX version:

Code:
/usr/xpg4/bin/id -un



Code:
$ uname -sr
SunOS 5.8
$ id -un
id: illegal option -- u
Usage: id [-ap] [user]
$ PATH="$(getconf PATH)" id -un
drado

# 6  
Old 10-12-2010
The username must be the first parenthetical:
Code:
awk -F'[()]' '{print $2}'

A smarter approach:
Code:
sed 's/^.*uid=[0-9]*(\([^)]*\).*/\1/'

Regards,
Alister
# 7  
Old 10-13-2010
Hi,

Another 'sed' solution:
Code:
id | sed 's/[^(]\+(\([^)]\+\)).*/\1/'

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing XML using command line

Hi Experts, How do I parse a XML with below contents <saw:user name="mbussey@xyz.com" /> <saw:user name="kimmy.chan@pqr.com" /> <saw:user name="chudgins@gmail.com" /> and retrieve below output ? mbussey@xyz.com kimmy.chan@pqr.com chudgins@gmail.com ... (17 Replies)
Discussion started by: pauldx
17 Replies

2. Shell Programming and Scripting

Suggestions for command line parsing

Hi all I need to put a command line parser together to parse numeric fields and ranges passed to a script. I'm looking for a bash function that is as elegant and simple as possible. So the input would be of the following form - 1,2,8-12 This would return - 1,2,8,9,10,11,12 Input can... (7 Replies)
Discussion started by: steadyonabix
7 Replies

3. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Parsing a command line parameter in script

I have a simple script that builds a complex program call which passes a number of parameters to the program. I'm trying to enhance the script to include the value of the command line parameter in the name of a file being created. The problem I'm having is that the parameter may include a forward... (11 Replies)
Discussion started by: pbmax626
11 Replies

5. Shell Programming and Scripting

Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it. At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the... (3 Replies)
Discussion started by: Breakology
3 Replies

6. Shell Programming and Scripting

parsing return from cal command

Jim , Anyone I do not have GNU date Besides I am particularly interested in how one can parse the return from the cal command. Say do - cal 11 2008 - and parse out a given date, say the 8th and return that the 8th was Saturday. ( diffrentiating between S for Saturday and Sunday , also in the case... (1 Reply)
Discussion started by: dragrid
1 Replies

7. Shell Programming and Scripting

parsing output from a diff command...

hi, i have a script which pipes the output of a diff -rq command into a separate file/ it would read something like this: Files mod1/lala/xml/test1.txt and mod2/lala/xml/test1.txt differ Only in mod2/lala/xml: test2.txt What i need to do is to parse this file so i end up with just a... (5 Replies)
Discussion started by: kam
5 Replies

8. Shell Programming and Scripting

parsing output from ls command

I have to test the directory name which is obtained from for dir in `ls -l|grep $9 ' i need to check whether it is directory ( if yes, I have to check the first 3 character fo the directory how can I do that? Please help me thanks (3 Replies)
Discussion started by: ajaya
3 Replies

9. UNIX for Dummies Questions & Answers

command line argument parsing

how to parse the command line argument to look for '@' sign and the following with '.'. In my shell script one of the argument passed is email address. I want to parse this email address to look for correct format. rmjoe123@hotmail.com has '@' sign and followed by a '.' to be more... (1 Reply)
Discussion started by: rmjoe
1 Replies

10. UNIX for Advanced & Expert Users

Parsing the command line arguments

Is there a way to get the command line arguments. I am using getopt(3) but if the arguments are more than one for a particular option than it just ignores the second argument. For eg ./a.out -x abc def now abd will be got with -x using getopt "( x : )" and string abc\0def will get stored... (7 Replies)
Discussion started by: jayakhanna
7 Replies
Login or Register to Ask a Question