3/20/2002

Anatomy of a perl one-line substitution command
perl -i[.backup-extension] -p -e 's#pat1#pat2#ig' files

-i[.backup-extension]
Tells perl to run the command on the named files in-place, i.e., using the named files both as input and output. If a backup extension is provided, the unmodified version of each file will be saved with the extension appended.
Example: -i.bak
-p
Tells perl to assume an input loop around your one-line program and echo the output.
-e
The one-line program follows.
's#pat1#pat2#ig'
The perl "substitution" function. Matches every instance of the pattern pat1 and replaces it with pat2. The "#" used to delimit the patterns can be any character that isn't found in pat1 or pat2. The perl pattern matching used in pat1 is very powerful and somewhat complex; the main pitfall to remember is that you may need to escape special characters such as "." with a preceding backslash, e.g. "xyz\.rice\.edu". The trailing "i" flag means to ignore case when matching pat1. The trailing "g" flag means to apply the substitution multiple times on the same line (without the "g" it will only be applied to the leftmost pattern match on each line).
files
The file(s) on which the command should be run. In an HTML context, you probably want to specify a pattern in the shell to match your HTML files, taking into account any subdirectories you also want to include. Examples:
*.html

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home