0

I am trying to delete a specific entry in history. Clarification--by that I mean deleting all occurences containing a given command/or text. I used Aliteralmind's hxf script:

:<

Examples
    hxf "rm -rf"


#The unalias prevents odd errors when calling". ~/.bashrc" (May result in
#"not found" errors. That's okay).
unalias hxf

hxf()  {
read -r -p "About to delete all items from history that match \"$1\".      
Are you sure? [y/N] " response
response=${response,,}    # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
    #Delete all matched items from the file, and duplicate it to a temp
    #location.
    echo -e "grep -v \"$1\" \"$HISTFILE\" > /tmp/history"
    grep -v "$1" "$HISTFILE" > /tmp/history

    #Clear all items in the current sessions history (in memory). This
    #empties out $HISTFILE.
    echo "history -c"
    history -c

    #Overwrite the actual history file with the temp one.
    echo -e "mv /tmp/history \"$HISTFILE\""
    mv /tmp/history "$HISTFILE"

    #Now reload it.
    echo -e "history -r \"$HISTFILE\""
    history -r "$HISTFILE"     #Alternative: exec bash
else
    echo "Cancelled."
fi
}

(from https://unix.stackexchange.com/a/178956/115826)

However after running the hxf script, history gives me: (for example)

401 254: 263: 287: 288: 293: cd ~
402 203: 243: 290: 309: 315: 332: 370: 377: 389: 400: ls
403 200: 207: 255: 263: 302: 325: 359: 378: 385: 393: 397: 399: nedit

How to modify the hxf() script to eliminate history displaying a list of indices for each occurrence of a command? After running hxf() I get the above. Prior to running it, history gives:

401 cd ~
402 ls
403 nedit

The list of entries gets quite long and unreadable quickly. I need for history output to appear as it did prior to running hxf with the only difference being the command(s) I deleted with hxf() now removed.

peinal
  • 111

1 Answers1

2

If you are asking how to delete a certain command from the history:

history -d (number)

Get the number from running 'history'

Baazigar
  • 730
  • 1
    Not exactly. How to delete all occurences of a certain command from the history is the goal. – peinal May 19 '15 at 12:38
  • The answer is still history -d (number). You want numerous instance? history | grep (your string) | awk '{print$1}' | while read i; do history -d $i; done – Baazigar May 19 '15 at 14:49
  • @Baazigar: that has printed "history position out of range" a bunch of times. – Dan Dascalescu Jun 06 '20 at 19:25