Re: "Convert" perl command line to simple script



bluez34me@xxxxxxxxx wrote:
Here's an example of a script that I'm running to clean up html pages
that I'm generating automatically:

perl -pi -e 's/..\index/index/g' *.html

Is there some "standard" and simple way to get this into script form,
or do I need to write a complete script that handles looping through
the files, does the regex handling and all that?

Well, let's check what those command line parameters do (from perldoc
perlrun):
-e *commandline*
may be used to enter one line of program. If -e is given, Perl will
not look for a filename in the argument list. Multiple -e commands
may be given to build up a multi-line script. Make sure to use
semicolons where you would in a normal program.

Ok, that's trivial.

-i[*extension*]
specifies that files processed by the "<>" construct are to be
edited in-place. It does this by renaming the input file, opening
the output file by the original name, and selecting that output
file as the default for print() statements. [...]

Seems this is one you will have to code yourself.

-p causes Perl to assume the following loop around your program, which
makes it iterate over filename arguments somewhat like sed:

LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}

This one you will have to implement yourself, too (although that should be
rather trivial given this template).

And of course you will need to loop through all the file names resulting
from the shell glob expansion.

jue


.



Relevant Pages

  • Re: beginners question
    ... Your script works fine. ... Using the for loop, that line has already been read so you do not need to ... line to extract the words. ... write True or False to your output file ...
    (comp.lang.python)
  • RE: Why wont my script terminate?
    ... Why won't my script terminate? ... Laurie, here are a couple of other things to look at: ... You are not explicitly checking for errors on your DBI calls after the ... > msgs in output file when testing) ...
    (perl.dbi.users)
  • RE: Why wont my script terminate?
    ... No, it's not very big--might be kinda ugly to you expert Perl programmers, ... to this list (DBI version 1.14 written by Tim Bunce for MsWin32). ... Here's my script: ... 'TEST_' to output file names.) ...
    (perl.dbi.users)
  • Perl script to search and replace a string in a file
    ... replaces the string in the input file with some other string and writes out ... the output file. ... I want to change the script to traverse through a directory of files ...
    (perl.beginners)
  • Re: Need a good Unix script that..
    ... I'm trying to write a script to delete all line that include a certain ... I have a constant reoccurring lines within the output file ... that fits a certain pattern but it does not appear to remove anything. ... sed <- simple extraction / replacement ...
    (freebsd-questions)