TheWorldPeace has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Gurus; I am recently assigned to modify hundreds of unix files by inserting few snipets of code after certain lines containing certain strings. The insertable snipets and the strings are varied depending on the filename. I thought about using a Unix scripting language to automate the mechanical task. However, I am a C++ and C programmer and don't know much about Perl, Awk, or Sed. I just know they exist. I staretd to learn Perl today using an online tutorial. However due to that time pressure, I was wondering if there is any book, website or anyplace that would have some code already doing the same thing, except maybe for minor modifications? Thanks in advance
  • Comment on Manipulating File Contents Interactively

Replies are listed 'Best First'.
Re: Manipulating File Contents Interactively
by hbm (Hermit) on Dec 22, 2008 at 20:10 UTC

    I often combine Unix's find-exec and perl-pie like this:

    $ find /path/to/files -name "*.xml" -exec perl -p -i -e 's/this/that/g +' {} \;

    Depending on the snippet/string/filename relationship, you may be able to repeat the above, just changing find's -name and perl's "this" and "that":

    $ find /path/to/files -name "*.txt" -exec -perl -p -i -e 's/these/thos +e/i' {} \;

    Or you can create a simple perl or other script and use find to pass in the filename:

    $ find /path/to/files -type f -exec changeIt.pl {} \;
Re: Manipulating File Contents Interactively
by Anonymous Monk on Dec 22, 2008 at 19:54 UTC
    As ikegami said, right up Perl's alley, and there might be something very close to what you need in Christiansen & Torkington's Perl Cookbook (O'Reilly, ISBN 0-596-00313-7). I think you will find the effort to learn Perl will be repaid, especially when you find yourself working in the context of rapid development.

    However, as this is a professional application, it is important that it be provably correct. Like most people, I, and I suspect you, learn by making mistakes. Given the possibly intense time pressure you are under, do you want to wind up doing your 'learning' in front of your boss? It might be safer to 'just do it' in a language you know, i.e., C or C++, and learn Perl at a more leisurely pace in preparation for the next rush assignment.

Re: Manipulating File Contents Interactively
by hangon (Deacon) on Dec 22, 2008 at 19:58 UTC

    Here's some example code snippets. Hopefully it will help narrow down what you need to focus on for this project.

    # start your program with these. trust me use strict; use warnings; # hash table of search strings and insertion code my %insertions = ( 'search string' => 'code insertion', foo => 'bar', alpha => 'beta', ); # read in array of filenames from a directory opendir (my $DH, $directory); my @files = $readdir $DH; closedir $DH; # iterate over array of filenames for my $filename (@files){ # process your file here # or use a subroutine subName($filename); } # basic subroutine sub subName{ # get first arg passed my $filename = shift; # if you need a return value return $foo; } # opening files. $TFH = target filehandle open (my $TFH, ">", $targetfile); open (my $SFH, "<", $sourcefile); # read source file line by line while (my $line = <$SFH>){ print $TFH, $line; # use regular expression to find matching string if ($line =~ /$matchString/){ print $TFH, $snippet; } } close $TFH; close $SFH;
Re: Manipulating File Contents Interactively
by ikegami (Patriarch) on Dec 22, 2008 at 19:01 UTC
    Sounds trivial to implement in Perl, given more details
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Manipulating File Contents Interactively
by Dru (Hermit) on Dec 23, 2008 at 18:31 UTC
    Not sure what online tutorial you are using, but I would recommend this online book: Beginning Perl

    Although it is a bit dated, it is still very relevant. I wish I had it when I first started learning Perl.

    Thanks,
    Dru

    Perl, the Leatherman of Programming languages. - qazwart
Re: Manipulating File Contents Interactively
by Tanktalus (Canon) on Dec 24, 2008 at 00:24 UTC

    This is precisely how I started using perl - making small and huge changes to hundreds or thousands of files simultaneously (which then made me think about how to make it such that such silliness wasn't repeated in so many files in case it changed again - which it has). I knew sed and awk, but they got far too convoluted far too quickly. I started with perlintro and went from there. Coming from C++, you should have no problem (which is my background as well, though I had been doing some shell scripting, too).