Email ids from gecos

I would like to extract only the email ids from the gecos of each user id. I have to get the email ids of all the users on the server like this.
Can someone please assist me with the command/script?

Can you give us the example of what you are expecting with sample data

Here is an example. When you do a

lsuser <username>

you have a field called gecos which has values like this

 gecos=GOPALAKRISHNAN, GAYATHRI; 123/P/11111 ;gg@abc.com

Like this, it will be there for every user id on the system. I would like to get only the email id portion of the gecos field extracted.

lsuser <username> | awk '/^gecos=/ {print $NF}' FS=';'

It's not working. Sorry.

Whats the output you get when you run the above command

---------- Post updated at 04:19 AM ---------- Previous update was at 03:59 AM ----------

Also, please provide the output of lsuser <username>. I dont have that installed on our system / I dont have sccess to it.
When I did testing with the data you have provided, it worked for me.

$ echo "Srini
> gecos=GOPALAKRISHNAN, GAYATHRI; 123/P/11111 ;gg@abc.com
> Random line again" | awk '/^gecos=/ {print $NF}' FS=';'
gg@abc.com
$

Output is nothing. It just returns to the prompt.

As I have shown you, I could see the output from the code...
Still I suggest, please show us the entire output as is of "lsuser <username>", we shall try to help

---------- Post updated at 02:57 PM ---------- Previous update was at 02:54 PM ----------

Try this and confirm

lsuser <username> | awk '/gecos=/ {print $NF}' FS=';'

or

lsuser <username> | awk '/^[ \t]*gecos=/ {print $NF}' FS=';'

What you call "gecos" field usually is the "user name or comment field", field 5 in the : separated /etc/passwd file. Try

awk -F: '{sub(/^.*;/,"",$5);print $5}' /etc/passwd

From the command "lsuser" and the general output format i deduce it is an AIX system (still, it would be nice to state that instead of letting the audience exert its collective criminalistic skills). The output of "lsuser" is a space-separated list of user properties in the form "property=value".

It is possible to limit the output to one (or several) specific properties, therefore:

lsuser -a gecos <username>

will limit the output to only the gecos property (and the username, which is always part of the output). From there it should be easy to extract everything up to the delimiting semicolon by shell expansion:

var="$(lsuser -a gecos <username>)"
print - ${var#*;}

I hope this helps.

bakunin

cat /etc/passwd | awk -F: '($1=="'$1'"){print $5}' | awk -F\; '{print $3}'

That is a useless use of cat. Also, if you're doing awk | awk | awk, you might as well put it all in one awk.

Also, never use creative quoting to get variables into awk, that's unsafe and there's no need.

awk -F: '($1==U){sub(/.*;/, "", $5); print $5 }' U="$1" /etc/passwd

try this

lsuser -a gecos <username> | cut -d ';' -f3

gecos, not "user name or comment field" is the correct name for this field. Entries in this field should be separated by commas and such entries can be changed by a user using the chfn(1) utility.

@fpmurphy: Historically, you are right. But, on my - admittedly linux - system, the man page reads:

. I'll nevertheless try to remember next time and rephrase my answer.

May be you can try out with below regex:

grep -Eo "[a-zA-Z0-9]+@[^ ]+\.[a-zA-Z0-9]+"

e.g.

lsuser <username> | grep -Eo "[a-zA-Z0-9]+@[^ ]+\.[a-zA-Z0-9]+"