Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Qusetion about awk

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Hello,

I'm trying to use awk to parse the uid=xxx information that is displayed when I run the command id $user. Right now my code looks like

Code:
#!/bin/bash
                                                                                                                                                                
id $USER | awk '/uid=[0-9]*/ {print $1}'

Which prints out:
uid=500(jsmith)


My question is how do I get awk to just print out the uid=xxx portion of the 'id $USER' command. In other words to leave off the jsmith portion. Any help is greatly
appreciated.



 
Perhaps you can use: [tt] id -u $USER[/tt]
 
Thanks for the tip Ygor. It worked. Now I need a way of displaying the "real name" of the user. I thought about using the finger command with something like
Code:
finger | awk '// {print $1}'

However this will print the name of the user several times if they have more than one terminal open. Can anyone suggest another way for me to accomplish this. Any help is always appreciated.

Thanks!
 
I think finger just does a look-up to /etc/passwd - you could do something similar...
[tt]
USERID=$(id -u $USER)
USERNAME=$(awk -F: "$USERID"'==$3{print $5}' /etc/passwd)
[/tt]

 
What about simply this ?
awk -F: '$1=="'"$USER"'"{printf "%s (uid=%d,gid=%d) %s\n",$1,$3,$4,$5}' /etc/passwd

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
A couple of ways:

Code:
id $USER | exec 3<&0
while read -u3 line; do
   print ${line%%\(*}
done

Code:
id $USER | awk -F'(' '{print $1}'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top