Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Favourite One-liners?

by radiantmatrix (Parson)
on Jun 28, 2005 at 21:09 UTC ( #470824=note: print w/replies, xml ) Need Help??

in reply to Favourite One-liners?

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:
perl -pe 'next if /^(\s*)#/' file.conf > clean-file.conf"
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 copy.

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:

perl -e "for(glob '*.xls'){$o=$_; s/(\.xls)$/.old$1/i;rename $o,$_}"
works smashingly (specifically for XLS files, that one).

And finally, also on Windows (where find 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:

perl -e "for(glob '*'){next unless -f;@s=stat;unlink($_) if (time-$s[9 +])>60*60*48;}"
Update: And to search the whole tree:
perl -MFile::Find -e "find(sub{@s=stat;unlink if (time-$s[9])>60*60*48 + && -f},'.')";

Yoda would agree with Perl design: there is no try{}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://470824]
[Eily]: congrats Discipulus :)
[choroba]: I often can't log in to pause. But after it fails for the first time, everything works for the second time.
[cavac]: Ok, thanks for the information

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-02-22 09:06 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (325 votes). Check out past polls.