Your Ad Here

Senin, 26 Desember 2011

Unix Tip: EAT YOUR PERL PIE

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

UNIX GURU UNIVERSE
UNIX HOT TIP

Unix Tip 3608 - December 26, 2011

http://www.ugu.com/sui/ugu/show?tip.today

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


EAT YOUR PERL PIE

Mom always sed, "eat your Perl pie"!

A common task is to substitute
text strings within one or
more files. The sed string
substitution command is often
used to accomplish this, where
sed is passed a file name, the
string substitutions are written
to another file, and then that
file is copied over the first
file to effect the changes
desired in the original.

This approach has some inherent
problems. The command syntax is
lengthy and cumbersome, a
secondary file is created
which involves additional disk
I/O, and when that file is
copied or moved back over the
first one, permissions problems
and file ownership problems can
be created. If your umask
doesn't match the permissions of
the original file, then the
permissions of the final modified
file will be different than those
of the original. Likewise,
default ownerships and group
names are imposed on the temporary
copy, and if one is not careful,
when the temporary copy is written
back to the original file, these
can destroy the original file's
ownership and group information.

A much better way to accomplish
text substitution within a file or
group of files is to use the
"perl pie" approach, as shown here
on file foo:

perl -p -i -e 's/original text string/replacement string' foo

The above command will replace the
first instance of "original text
string" with "replacement string"
in file foo. If you want to
perform this globally within the
file, add the /g global specifier
at the back end, as follows:

perl -p -i -e 's/original text string/replacementstring/g' foo

To act on several files in the
same directory, change the file
specification as needed, by
specifying foo*, *, or whatever
you need.

Note that perl uses the exact
same syntax for the actual string
substitution portion of the command
as sed does, which makes the command
syntax easy to remember. However,
perl performs the entire operation
on the file without the use of any
secondary files needing to be
created, which eliminates the extra
disk I/O and the potential
permissions and ownership issues.

This tip generously supported by: jem@postfive.rose.hp.com


--------------------------------------------------------------------------
To Subscribe: http://www.ugu.com/sui/ugu/show?tip.subscribe
To Unsubscribe: http://www.ugu.com/sui/ugu/show?tip.unsubscribe
To Submit A Tip: http://www.ugu.com/sui/ugu/show?tip.today

==========================================================================
DISCLAIMER: All UNIX HOT TIPS ARE OWNED BY THE UNIX GURU UNIVERSE AND ARE
NOT TO BE SOLD, PRINTED OR USED WITHOUT THE WRITTEN CONSENT OF THE UNIX
GURU UNIVERSE. ALL TIPS ARE "USE AT YOUR OWN RISK". UGU ADVISES THAT
ALL TIPS BE TESTED IN A NON-PRODUCTION DEVELOPMENT ENVIRONMENT FIRST.

Unix Guru Universe - www.ugu.com - tips@ugu.com - Copyright 1994-2001
==========================================================================

Tidak ada komentar:

Posting Komentar

Your Ad Here
Free Automatic Backlink Free Auto Backlink Free Auto Backlink Free Auto Backlink Free Auto Backlink Free Auto Backlink Free Auto Backlink Free Auto Backlink Free Auto Backlink Free Auto Backlink