awk/sed/ksh script to cleanup /etc/group file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk/sed/ksh script to cleanup /etc/group file
# 1  
Old 02-25-2008
awk/sed/ksh script to cleanup /etc/group file

Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted.
This happened due to manual manipulation of /etc/passwd files.
I need to do this for 40 servers.
Can anyone help me in achieving this? Even reducing a step or two will be greatly appreciated.
Thanks in advance.
# 2  
Old 02-25-2008
Hope Perl is OK. Smilie

file1:
Code:
matt:1:2:3
kristen:1:2:3
curt:1:2:3
jennifer:1:2:3

file2:
Code:
jennifer:1:2:3
kristen:1:2:3
curt:1:2:3

Here's the code:
Code:
#!/usr/bin/perl

# names in file1 not in file2
foreach (@names=`cut -d ':' -f1 file1`) {
        chomp(@names);
        $count = `grep -c $_ file2`;
        if ($count == 0)        {
                print "$_ is in file1 but not file2.\n";
        }
}

$ ./cleanup.pl
matt is in file1 but not file2.
$

Of course there's probably a really simple way to do it...I'll leave that to much smarter people.

HTH
# 3  
Old 02-26-2008
Another solution:
Code:
awk ' FNR==NR{for (h=4;h<=NF;h++)a[$h]=n;next}b[$1]=n
END {for (i in a)if ((!(i in b))&&(i != "")) print i" in group file but not in passwd"
}' FS="(:)|(,)"  /etc/group /etc/passwd


Last edited by Klashxx; 02-26-2008 at 05:06 AM.. Reason: optimization
# 4  
Old 02-26-2008
Cool, Klashxx I'm going to study your script. Thanks for the reply.
# 5  
Old 02-26-2008
Thanks a bunch!

Thank you all for providing me the script to take care of what I need to do.
Smilie
# 6  
Old 02-27-2008
One more thing...

Thank you for everything... but I forgot to mention that I need to get rid of only users that starts with a alpha character followed by 4 digits (ie. A9999).
Can you do this with awk or do I have to use ksh?
Thanks.
# 7  
Old 02-28-2008
Hi , u can use:
Code:
awk ' FNR==NR{for (h=4;h<=NF;h++)if(match($h,/^[A-Za-z][0-9]{4}$/))a[$h]=n;next}b[$1]=n
END {for (i in a)if ((!(i in b))&&(i != "")) print i" in group file but not in passwd"
}' FS="(:)|(,)" /etc/group /etc/passwd


Last edited by Klashxx; 02-28-2008 at 03:56 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using sed to change file into an awk script

Hi I want to use sed to change a text files input into an awk script. For example if the input says " chord -- english " I want to change this using sed 's/pattern 1 /pattern 2 /'g filename but I don't understand how to use part of the pattern 1 to input that into pattern 2 . Like after... (10 Replies)
Discussion started by: enforcer
10 Replies

2. Shell Programming and Scripting

How to pick a group of data using awk/ksh

Hi gurus, I have data coming in as shown below. And in each case, I need to pick the data in the last group as shown below. Data Set 1: DC | 18161621 LA | 15730880 NY | 16143237 DC | 18161621 LA | 17316397 NY | 17915905 DC | 18161621 LA | 17993534 NY | 18161621 DC | 18161621... (11 Replies)
Discussion started by: calredd
11 Replies

3. Shell Programming and Scripting

KSH: substitution character, AWK or SED?

Hi Gurus, I am working with a korn shell script. I should replace in a very great file the character ";" with a space. Example: 2750;~ 2734;~ 2778;~ 2751;~ 2751;~ 2752;~ what the fastest method is? Sed? Awk? Speed is dead main point, Seen the dimensions of the files Thanks (6 Replies)
Discussion started by: GERMANICO
6 Replies

4. Shell Programming and Scripting

Have to learn sed and awk commands in kSH?

Links Please??? (2 Replies)
Discussion started by: Diddy
2 Replies

5. Shell Programming and Scripting

Mail cleanup from ksh script, keeping 50 most recent msgs

I found some posts describing how to completely clean out a mailbox in Unix/Linux. But I want to keep the 50 most recent messages. Any ideas out there? Thanks! (3 Replies)
Discussion started by: OPTIMUS_prime
3 Replies

6. Shell Programming and Scripting

KSH to group records in a file and compare it with another file

Hi, I've a file like below: DeptFile.csv DeptID EmpID ------- ------ Dep01 Emp01 Dep01 Emp02 Dep01 Emp03 Dep02 Emp04 Dep02 Emp05 I've another file which has EmpFile.csv EmpID Salary ------ ------ (3 Replies)
Discussion started by: Matrix2682
3 Replies

7. Shell Programming and Scripting

AWK or KSH : Sort, Group and extract from 3 files

Hi, I've the following two CSV files: File1.csv File2.csv Class,Student# Student#,Marks 1001,6001 6002,50 1001,6002 6001,60 1002,7000 ... (3 Replies)
Discussion started by: Matrix2682
3 Replies

8. Shell Programming and Scripting

HELP! Group by in shell script (awk/sed?)

Hello, Could some expert soul please help me with this? I have following file format - task time abc 5 xyz 4 abc 5 xyz 3 ddd 10 ddd 2 I need to generate output as - task ... (5 Replies)
Discussion started by: sncoupons
5 Replies

9. Shell Programming and Scripting

help with ksh/awk/sed script, random # of fields

Hello all, I'm working on an attendance callout script for a school district. I need to change our current layout for the vendor. Currently the data is in the form of: studentid,period,building, Heres a sample of some made up records: 500,1,30, 500,2,30, 500,3,30, 500,6,30,... (7 Replies)
Discussion started by: axo959
7 Replies

10. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies
Login or Register to Ask a Question