1825803 Members
2616 Online
109687 Solutions
New Discussion

Test Processing

 
SOLVED
Go to solution
Waqar Razi
Regular Advisor

Test Processing

I have 3 passwd files. Some of the users exist in 3 files (they are from 3 different servers) and some of them only exist in only one file.

I want to make a single file taking users entries from 3 files combine into a single file. For users who exist in all the 3 servers, I want to list their 3 uids infront of their user names. Can some one give me some direction how to do that?
8 REPLIES 8
Steven E. Protter
Exalted Contributor

Re: Test Processing

Shalom,

cat passwd | awk -F: '{print $1}'

Use this command to make lists.

diff the files and use sort -u to come up with a single, valid master user list.

Then combine the files. Probably best to cat them into one file and delete the dups.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Prashanth Waugh
Esteemed Contributor

Re: Test Processing

Hi Razi,

U can use the below command for 3 passwd file. then take the output of each file into same xl sheet then sort it by name. After that u can make one file .But before doing any activity like replacing the file first take the backup of that file.

the command is
cat /etc/passwd | awk -F: '{print $1 ,$3}'

Best Luck
Atul
For success, attitude is equally as important as ability
Rasheed Tamton
Honored Contributor

Re: Test Processing

Is it what you want:

awk -F: '{print $3, $1}' firstfile secfile thirdfile |sort -k2 > userlist
Waqar Razi
Regular Advisor

Re: Test Processing

Let me give you more detail:

Lets say I have a user called steve on three servers having different uids like 108, 120, 139 on three different servers but same username steve on all of them, and on the other hand, lets say I have another user kami on just one server having uid 159, I want my resulting file to look like that.

steve 108 120 130
kami 159

Can some one help me in this?
James R. Ferguson
Acclaimed Contributor
Solution

Re: Test Processing

Hi Wager:

# cat .allusers
#!/usr/bin/perl
use strict;
use warnings;
my ( %users, $name, $uid );
while (<>) {
my ( $name, $uid ) = ( split /:/ )[ 0, 2 ];
push( @{ $users{$name} }, $uid );
}
for $name ( sort keys %users ) {
print "$name: @{$users{$name}}\n";
}
1;

...run, passing any number of files as arguments, like:

# ./allusers /etc/passwd /etc/passwd.server2 /etc/passwd.server3

...output looks like:

ablebody: 300
somebody: 333
wagar: 111 1221
zebra: 101 102 101

Regards!

...JRF...
Tim Nelson
Honored Contributor

Re: Test Processing

A basic way. I am sure there is something more elegant. Might just give you some ideas.


awk -F: '{print $1 " " $3}' passwd1|while read username uid
do
echo "File1: $uid $username"
echo "File2: \c"
awk -F: '$1 ~ "$username" {print $1 " " $3}' passwd2
echo "File3: \c"
awk -F: '$1 ~ "$username" {print $1 " " $3}' passwd3
done
Dennis Handly
Acclaimed Contributor

Re: Test Processing

Try this:
sort passwd1 passwd2 passwd3 | awk -F: '
BEGIN { getline; name = $1; uid = $3 }
{
if ($1 == name) {
uid = uid " " $3 # concat
} else {
print name, uid # print last
name = $1; uid = $3
}
}
END { print name, uid } '
Waqar Razi
Regular Advisor

Re: Test Processing

Lst script is working fine, thank you very much guys for your help and support.

I need one more help, Lets say if I wanna add when that particular user logs in last time, how can I add that to this script.

Lets say if I have a user steve and he logged in for the last time one week before, output should be like that:

Steve uid1 uid2 uid3 19-AUG-08