home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!gdt!brispoly!c_brown
- From: c_brown@csd.brispoly.ac.uk (Chris Brown)
- Newsgroups: comp.lang.perl
- Subject: How do I edit files with Perl
- Message-ID: <1992Sep15.114046.1416@csd.brispoly.ac.uk>
- Date: 15 Sep 92 11:40:46 GMT
- Sender: usenet@csd.brispoly.ac.uk (Usenet poster for nntp (tj - 17/12/91))
- Reply-To: c_brown@csd.bristol-poly.ac.uk
- Organization: Bristol Polytechnic (The Magic Roundabout)
- Lines: 48
- Nntp-Posting-Host: c_brown@192.35.175.2
-
- I have a script which generates password file details and creates a home
- directory for new user accounts.
- I need to modify my script to ensure the contents of input data files will
- be unique (in particular the logname) to the password file before altering
- the password file.
-
- I've written a noddy script to test my ideas.
- What I've decided to do is grep the password file for lognames obtained from
- the input files. If I come across any I save them to a list and print the
- details in a file for later reference.
- I then use a line adapted from the "Common Tasks with Perl" chapter to remove
- the line in each file where the offending string occurs.
-
- $SysFile = "/home/staff/csm/c_brown/Scripts/Perl_scripts/pass";
- $Tmp = "/home/staff/csm/c_brown/Scripts/Perl_scripts/";
- $Dups = "/home/staff/csm/c_brown/Scripts/Perl_scripts/dups";
- @WordList = ();
-
- # This section finds the duplicates
- $Date = `date`;
- @data_files = (test_data1, test_data2, test_data3);
- open(Duplicates, ">>${Dups}");
- print Duplicates $Date;
- foreach $new_data (@data_files) {
- print STDOUT "Doing $new_data .... \n";
- open(NewData, "+<${Tmp}${new_data}");
- while(<NewData>) {
- ($Login) = split;
- chop($crud = `grep $Login $SysFile`);
- if ("$crud" ne "") {
- print Duplicates $_;
- @WordList = (@WordList, $Login);
- }
- }
- close(NewData);
- }
-
- # This section removes the offending lines
- foreach $word (@WordList) {
- `perl -pi.bak -e 's/${word}\\n//' @data_files[0 .. $#data_files]`;
- }
-
- My questions are:
- 1. Does anyone know of a neater way of achieving the same goal?
- 2. Why does `perl -pi.bak -e 's/${word}\\n//' @data_files[0 .. $#data_files]`;
- only work with the -pi.bak option? I don't really want the .bak files.
- 3. Why does the same line process the .bak files? Files test_data1.bak and
- test_data2.bak are exactly the same as test_data1 and test_data2 respectively.
-