bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Purge History of rm commands


From: Dennis Williamson
Subject: Re: Purge History of rm commands
Date: Tue, 20 Sep 2011 08:58:34 -0500

On Tue, Sep 20, 2011 at 1:34 AM, Roger <rogerx.oss@gmail.com> wrote:
>> On Mon, Sep 19, 2011 at 08:38:44AM -0400, Greg Wooledge wrote:
>>On Sun, Sep 18, 2011 at 10:11:17PM -0800, Roger wrote:
>>> > On Mon, Sep 19, 2011 at 01:37:22AM -0400, Mike Frysinger wrote:
>>> >On Monday, September 19, 2011 01:18:02 Roger wrote:
>>> >> I'm stumped on this as my history is in the format of:
>>> >>
>>> >> $ tail ~/.bash_history
>>> >> #1316296633
>>> >> man bash
>>> >> #1316296664
>>> >> bash -xv
>>> >> #1316372056
>>> >> screen -rd
>>> >> #1316375930
>>> >> exit
>>> >> #1316392889
>>> >> exit
>>
>>> >gawk '{ c = $0; getline; if ($1 != "rm") { print c; print; } }' 
>>> >.bash_history
>>>
>>> I don't know gawk (yet), but thinking this isn't going to step-up the one 
>>> line
>>> from the found 'rm' instance and omit the comment above it. (see above)
>>
>>Why not try running the code before you claim it doesn't work?  Here,
>>written out in a more traditional-looking format:
>>
>>gawk '
>>{
>>  c = $0
>>  getline
>>  if ($1 != "rm") {
>>    print c
>>    print
>>  }
>>}
>>'
>>
>>Does that make it easier to read?
>
>
> Just got time to test and am amazed it works better then I expected, even 
> after
> trying to trace the script.
>
> $ diff .bash_history.test .bash_history.gawk
>
> 1,2d0
> < #1315341860
> < rm test
>
> It caught the above 'rm' test statement I inserted and also left alone all
> "maildirmake" commands.  However, it missed the lines with " rm " in the
> middle. (ie. blah blah command; rm bleh; command blah)
>
> No matter though, the GAwk snippet is posted and verified for others trying to
> purge rm from their history.  They can now do so without clearing their whole
> history.
>
> I've done some minimal research on the history of GAwk/Awk and understand it's
> powerful, and how it recently had it's code optimized.  Then GNU Manuals are
> great reads on the Kindle DXG, and will likely be studying GAwk next.
>
> --
> Roger
> http://rogerx.freeshell.org/
>
>

You can get as fancy as you want with regexes in order to catch all
cases of "rm" anywhere on the line and reduce the chances of a false
positive:

gawk '{ c = $0; getline; if ($0 ~
(^|;[[:space:]]*)\<rm\>[[:space:]]+/) { print c; print; } }'
.bash_history

might come a little closer, but it would falsely catch "echo 'There
will be cake; rm 414 is reserved for the party'". It lets your
"maildirmake" through.

It searches for "rm" anywhere on the line as a "word" (and followed by
at least one space or tab) at the beginning of the line or after a
semicolon and optional spaces or tabs.

-- 
Visit serverfault.com to get your system administration questions answered.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]