note
radiantmatrix
I occasionally like to remove whole-line comments from config files or Perl scripts (esp. when the comments prevent me from seeing the code clearly). This works great:
<code>
perl -pe 'next if /^(\s*)#/' file.conf > clean-file.conf"
</code>
Remove the (\s*) if your definition of "whole-line comment" doesn't include whitespace before the '#'. Also note that I deliberately avoid in-place edit, as the point is to make a clean <em>copy</em>.
<p>On Windows, I often like to backup generated files before doing a second test run. Since I want to compare both sets of output, sometimes I need to leave the extension intact:
<code>
perl -e "for(glob '*.xls'){$o=$_; s/(\.xls)$/.old$1/i;rename $o,$_}"
</code>
works smashingly (specifically for XLS files, that one).
<p>And finally, also on Windows (where <tt>find</tt> means something different than on UNIX), I sometimes want to remove all files in a directory (not the whole tree) that are more than 48 hours old:
<code>
perl -e "for(glob '*'){next unless -f;@s=stat;unlink($_) if (time-$s[9])>60*60*48;}"
</code>
<b>Update:</b> And to search the whole tree:
<code>
perl -MFile::Find -e "find(sub{@s=stat;unlink if (time-$s[9])>60*60*48 && -f},'.')";
</code>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-375088">
<p align='right'><small>
Yoda would agree with Perl design: there is no <tt>try{}</tt>
<small></p>
</div></div>
470397
470397