- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script to extract passwd name field data into ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 04:50 AM
04-15-2008 04:50 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 05:24 AM
04-15-2008 05:24 AM
Re: Script to extract passwd name field data into a variable
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 05:38 AM
04-15-2008 05:38 AM
Re: Script to extract passwd name field data into a variable
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 06:46 AM
04-15-2008 06:46 AM
Re: Script to extract passwd name field data into a variable
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 08:21 AM
04-15-2008 08:21 AM
Solutionthis 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 10:05 AM
04-15-2008 10:05 AM