Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tlug: Lame program contained within; no flames please!



Here you go, Jim.  I ought to insert here that famous Star Trek line,
"He's dead, Jim."  Lame slow perl program, but my limited testing
shows it working.

It's in three parts.  A shell script (tlugadd.sh) pipes the output of
find into xargs which passes the file names to the perl script.  It's
uglier but easier.  The find assumes you want to modify all *.html
regular files from the current directory down.  Not sure how familiar
you are with find and xargs.  If all else fails, invoke the perl
program directly.

The perl program (tlugadd.pl) modifies all files named on the command
line.  There are two variables you care about, one more than the
other.  The important one is $magicline, which is the line AFTER which
the new data is inserted.  The first line it finds that matches this
and has nothing else on the line is the line in question.  The
matching is case independent.  The other variable is $newfile, which
is just the file name of data to add.  You probably don't need to
change it.

The last file (tlugadd.data) is what to insert.  It will be inserted
exactly as is.  I'm not sure what perl will do if the last line has no
newline, so that's the only thing to double check.  The data in it is
probably not what you want :-)

The files are rewritten in place.  No backups are kept.  I would most
certainly copy everything first, back it up, etc.  I would do this and
I wrote it!  If I don't trust myself, you shouldn't either :-)

Also it would probably be a good idea to try it directly on a single
file first.

So, steps to use:

1.  Put the three pieces in your path, in the top level directory,
    whereever you want.

2.  Fix up any #! paths in tlugadd.pl and tlugadd.sh.

3.  Make sure tlugadd.pl and tlugadd.sh have execute permission.

4.  Modify the find args in tlugadd.sh if you need to.

5.  Modify the $magicline variable in tlugadd.pl as necessary.

6.  Put the data you want in tlugadd.data.

7.  BACKUP or COPY your files, please, don't make me feel guilty!

8.  Copy a real data file and test it:

	cp real-data-file.html test-me.html
	tlugadd.pl test-me.html

9.  If that looks good, delete the test file and do it for real.

	rm test-me.html
	tlugadd.sh

10a. Scream and yell at me for whatever I goofed up :-)

-- or --

10b. Tell me it worked!

------------------------- tlugadd.sh ---------------------
#!/bin/sh

find . -type f -name '*.html' -print | xargs ./tlugadd.pl
------------------------- tlugadd.pl ---------------------
#!/usr/bin/perl -w

# Supply a list of file names.  Each file is opened, scanned for the
# magic marker, and rewritten with the new data inserted immedately
# following the magic marker line.

# This is the magic marker line.  It has to match completely, but case
# is ignored.  Don't include the trailing newline here.  This has to
# be a single line; any multi-line marker will simply hose things :-)

my($magicline) = qq`<P>`;




# This is the file name of the new data.

my($newfile) = "tlugadd.data";




# Collect the new data.

open (NEWDATA, "<$newfile") or die "Can't read new data file $newfile: $!";
my(@example.com) = <NEWDATA>;
close NEWDATA;


# Process all named files.

my($filectr) = 0;
my($skipctr) = 0;
my($filename);
foreach $filename (@example.com) {
    open (HTMLFILE, "<$filename") or die "Can't open file $filename: $!";
    my(@example.com) = ();
    my($line);
    my($inserted) = undef;
    while (defined($line = <HTMLFILE>)) {
	push (@example.com, $line);
	if (!$inserted && ($line =~ /^$magicline$/io)) {
	    $inserted = 1;
	    push (@example.com, @example.com);
	}
    }
    close HTMLFILE;

# Make sure it got inserted.

    if (!$inserted) {
	print STDERR "No magic marker line in $filename\n";
	$skipctr++;
	next;
    }

# Rewrite the file.  No backup is made.

    open (HTMLFILE, ">$filename") or die "Can't rewrite file $filename: $!";
    print HTMLFILE @example.com;
    close HTMLFILE;
    $filectr++;
}
print "Processed $filectr files\n";
print "Skipped $skipctr files\n" if $skipctr;
exit 0;
------------------------ tlugadd.data --------------------
*** Put Some Real Data Here ***
----------------------------------------------------------

-- 
            ... _._. ._ ._. . _._. ._. ___ .__ ._. . .__. ._ .. ._.
     Felix Finch: scarecrow repairman & rocket surgeon / felix@example.com
  PGP = 91 B3 94 7C E9 E8 76 2D   E1 63 51 AA A0 48 89 2F  ITAR license #4933
I've found a solution to Fermat's Last Theorem but I see I've run out of room o
--------------------------------------------------------------
Next Nomikai: 17 July, 19:30 Tengu TokyoEkiMae 03-3275-3691
Next Meeting: 8 August, Tokyo Station Yaesu central gate 12:30
*** 20 June: TLUG will be at the Tokyo Linux Fair
http://tlug.linux.or.jp/projects/linux-fair/fair.html
--------------------------------------------------------------
Sponsor: PHT, makers of TurboLinux http://www.pht.co.jp


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links