Operating System - HP-UX
1834049 Members
2100 Online
110063 Solutions
New Discussion

Re: Script to extract passwd name field data into a variable

 
SOLVED
Go to solution
Kurtkarl
Frequent Advisor

Script to extract passwd name field data into a variable

HI,
Just want to know if anyone out there has a readymade script wherein:
It extracts $5 (name description) from the /etc/passwd file and put those values into variables. I wanted to grab the first, middle and last name from $5 and store them into variables so we can use those variables to extract data from our database using sql. My problem here is, there are some login id’s from passwd files that has 2 words on it (first and last names) and some has 3 words (first, middle and last). Once I have those variables I can pass it on my Informix sql scripts to extract the data I needed.

I tried awk but I’m having a hard time figuring out how to handle the part that checks if $5 has 2 or 3 values on it. I tried using (if NF == 2) or (if NF == 3) conditions but still I’m not handling the loop so well.

Any help would be greatly appreciated.

Thanks
Just starting to learn thru this forum
5 REPLIES 5
Rasheed Tamton
Honored Contributor

Re: Script to extract passwd name field data into a variable

Hi,

FIRST=`awk -F: ' {print $5}' /etc/passwd |cut -f1 -d" "`

SEC=`awk -F: ' {print $5}' /etc/passwd |cut -f2 -d" "`

THIRD=`awk -F: ' {print $5}' /etc/passwd |cut -f3 -d" "`


You could extract using awk with split

awk -F: '{split($5,a," "); print a[1]}' /etc/passwd
awk -F: '{split($5,a," "); print a[2]}' /etc/passwd
awk -F: '{split($5,a," "); print a[3]}' /etc/passwd

Pass it to variables.

Regards.
James R. Ferguson
Acclaimed Contributor

Re: Script to extract passwd name field data into a variable

Hi:

Use 'split' which will return the number of elements it places in the array. For example:

# awk -F":" '{n=split($5,a,/,/);print n,"=",$5}' /etc/passwd

Regards!

...JRF...
Kurtkarl
Frequent Advisor

Re: Script to extract passwd name field data into a variable

Hi Rasheed,

Thanks for the reply. But how would I tell the script to check first how many fields that $5 has? Reason behind this is we have some entries on our passwd file $5 that has two entries (ex. James Dean) and some has 3 (ex. John H Cook). Your script will always assumed that field 2 is middle name wherein in some cases it is the last name.

Thanks again
Just starting to learn thru this forum
Peter Nikitka
Honored Contributor
Solution

Re: Script to extract passwd name field data into a variable

Hi,

this assumes, that a field containing only two words results in an empty middle name:

awk -F: '{n=split($5,nn," ")
if(n>2) {nm=nn[2]; n2=nn[3]}
else {nm="";n2=nn[2]}
printf("user=%s first=%s middle=%s last=%s\n",$1,nn[1],nm,n2)
}' /etc/passwd

If field5 contains more than 3 words: they are ignored.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Kurtkarl
Frequent Advisor

Re: Script to extract passwd name field data into a variable

thanks for all your replies. Really appreciate it. Peter, thanks for the script. This is what I needed.
Just starting to learn thru this forum