home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5938 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  2.3 KB

  1. Path: sparky!uunet!mcsun!uknet!gdt!brispoly!c_brown
  2. From: c_brown@csd.brispoly.ac.uk (Chris Brown)
  3. Newsgroups: comp.lang.perl
  4. Subject: How do I edit files with Perl
  5. Message-ID: <1992Sep15.114046.1416@csd.brispoly.ac.uk>
  6. Date: 15 Sep 92 11:40:46 GMT
  7. Sender: usenet@csd.brispoly.ac.uk (Usenet poster for nntp (tj - 17/12/91))
  8. Reply-To: c_brown@csd.bristol-poly.ac.uk
  9. Organization: Bristol Polytechnic (The Magic Roundabout)
  10. Lines: 48
  11. Nntp-Posting-Host: c_brown@192.35.175.2
  12.  
  13. I have a script which generates password file details and creates a home
  14. directory for new user accounts.
  15. I need to modify my script to ensure the contents of input data files will
  16. be unique (in particular the logname) to the password file before altering
  17. the password file.
  18.  
  19. I've written a noddy script to test my ideas.
  20. What I've decided to do is grep the password file for lognames obtained from
  21. the input files. If I come across any I save them to a list and print the
  22. details in a file for later reference.
  23. I then use a line adapted from the "Common Tasks with Perl" chapter to remove
  24. the line in each file where the offending string occurs.
  25.  
  26. $SysFile = "/home/staff/csm/c_brown/Scripts/Perl_scripts/pass";
  27. $Tmp = "/home/staff/csm/c_brown/Scripts/Perl_scripts/";
  28. $Dups = "/home/staff/csm/c_brown/Scripts/Perl_scripts/dups";
  29. @WordList = ();
  30.  
  31. # This section finds the duplicates
  32. $Date = `date`;
  33. @data_files = (test_data1, test_data2, test_data3);
  34. open(Duplicates, ">>${Dups}");
  35. print Duplicates $Date;
  36. foreach $new_data (@data_files) {
  37.   print STDOUT "Doing $new_data .... \n";
  38.   open(NewData, "+<${Tmp}${new_data}");
  39.   while(<NewData>) {
  40.     ($Login) = split;
  41.     chop($crud = `grep $Login $SysFile`);
  42.     if ("$crud" ne "") {
  43.       print Duplicates $_;
  44.       @WordList = (@WordList, $Login);
  45.     }
  46.   }
  47.   close(NewData);
  48. }
  49.  
  50. # This section removes the offending lines
  51. foreach $word (@WordList) {
  52.   `perl -pi.bak -e 's/${word}\\n//' @data_files[0 .. $#data_files]`;
  53. }
  54.  
  55. My questions are:
  56. 1. Does anyone know of a neater way of achieving the same goal?
  57. 2. Why does `perl -pi.bak -e 's/${word}\\n//' @data_files[0 .. $#data_files]`;
  58.    only work with the -pi.bak option? I don't really want the .bak files.
  59. 3. Why does the same line process the .bak files? Files test_data1.bak and
  60.    test_data2.bak are exactly the same as test_data1 and test_data2 respectively.
  61.