[olug] Unix Tip: EAT YOUR PERL PIE

m0ntar3 at cox.net m0ntar3 at cox.net
Tue Dec 17 22:28:03 UTC 2002


This'll do the same (syntax is uglier, will do all files under a particular directory (or spec the '-name regex')):

find . -type f -exec grep -q word1 {} \; \
           -exec ex - '+%s/word1/word2/g|wq!' {} \;


> 
> From: Unix Guru Universe <listserv at ugu.com>
> Date: 2002/12/17 Tue PM 02:00:52 EST
> To: olug at olug.org
> Subject: [olug] Unix Tip: EAT YOUR PERL PIE
> 
> 
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> 
> 			      UNIX GURU UNIVERSE 
> 			         UNIX HOT TIP
> 
> 			Unix Tip 2177 - December 17, 2002
> 
> 		    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 at 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 at ugu.com - Copyright 1994-2001
> ==========================================================================
> 
> _______________________________________________
> OLUG mailing list
> OLUG at olug.org
> http://lists.olug.org/mailman/listinfo/olug
> 




More information about the OLUG mailing list