Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing lines from a cron.allow file by using another file

Status
Not open for further replies.

jpor2003

Technical User
Jun 3, 2005
76
GB
Hi,

I am currently trying to remove redundant logins from the cron.allow file. At the moment I have a script that has produced a list of users by cross referencing the /etc/passwd file (Users still have logins) and the cron.allow file by using the diff command.

What I need to do now is with the file "cron_allow_to_be_removed.txt" to go through and where the name matches in the cron.allow file for the line(s) to be removed.

For example in the 'cron_allow_to_be_removed.txt' I have:

user1
user12
user23
user26

Thes users exist in the cron.allow file and thus need removing from that file.

Does anyone have any ideas?

Thanks in advance.
 
A starting point:
awk '
NR==FNR{a[$0];next}
!($0 in a)
' /path/to/cron.allow /path/to/cron_allow_to_be_removed.txt > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The [tt]comm[/tt] command will select or reject lines in one file based on lines in another. Do a [tt]man comm[/tt] for more info.

Something like...
Code:
comm -23 cron.allow cron_allow_to_be_removed.txt > cron.allow.new
mv cron.allow.new cron.allow

Hope this helps.
 
OOps, I missreaded the question.
Lines should be removed from cron_allow, right ?
awk '
NR==FNR{a[$0];next}
!($0 in a)
' /path/to/cron_allow_to_be_removed.txt /path/to/cron.allow > output.$$ && mv output.$$ /path/to/cron.allow

Sam, comm will works if both files are sorted.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top