Weblog entry #73 for ajt

history alias
Posted by ajt on Wed 14 Feb 2007 at 18:41
Tags: , ,

I often run a command and grep for something in the output list. One of the most common commands I use it history. After a while typing "history pipe grep foo " got a bit of a pain. So in collaboration with my LUG I came up with an alias, so I could do "h foo" and achieve the same. Yesterday I added some sorting to cut down the duplicates.

In case anyone wants it, it's shown below. Should work fine on any Bash like shell, I keep mine in my .bashrc.

function h() {
    if [ -z "$1" ]
    then
        history | grep -v "  h" | sed 's/[ \t]*$//' | sort -k 2 -r | uniq -f 1 | sort -n
    else
        history | grep -v "  h" | grep $1 | sed 's/[ \t]*$//' | sort -k 2 -r | uniq -f 1 | sort -n
    fi
}

 

Comments on this Entry

Posted by dkg (216.254.xx.xx) on Wed 14 Feb 2007 at 19:12
[ Send Message | View dkg's Scratchpad | View Weblogs ]
Have you tried ctrl+r under bash? On bash 3.1.17(1)-release (etch), it gives me a "reverse-i-search" which is an interactive grep backwards through my history. If i don't like the current match, i can just ctrl+r again to find the previous match.

[ Parent | Reply to this comment ]

Posted by dkg (216.254.xx.xx) on Wed 14 Feb 2007 at 19:19
[ Send Message | View dkg's Scratchpad | View Weblogs ]
I just tested this, and it appears to be a generic readline function, not limited to bash.

So, for example, if you use socat often to talk to/debug SMTP servers like this:

socat TCP4:servername:25 READLINE,history=/home/username/.smtp_hist
you can actually use the same ctrl-r to recall SMTP inputs from previous sessions, which will be stored in /home/username/.smtp_hist.

Any other readline-enabled tool should have a similar capability in it too. Well-implemented libraries are wonderful things.

[ Parent | Reply to this comment ]

Posted by dkg (216.254.xx.xx) on Thu 15 Feb 2007 at 17:57
[ Send Message | View dkg's Scratchpad | View Weblogs ]
After writing this, i realized that i do this sort of socat thing way too much by hand. Inspired by ajt's posting of a bash snippet, i just set up my own bash function:
# make a TCP connection to a standard service, using readline to track your history: 
# example usage: 
#   hsrv http www.example.com
# you can specify an alternate port by appending it:
#   hsrv smtp mailserver.example.com 2525
# FIXME: which services besides smtp need the crnl option?
function hsrv() {
    [ -d "$HOME/.histories" ] || mkdir "$HOME/.histories"
    local extraopts=""
    [ "smtp" == "$1" ] && extraopts=",crnl"
    socat "READLINE,history=$HOME/.histories/$1$extraopts" "TCP4:$2:${3:-$1}"
}

[ Parent | Reply to this comment ]

Posted by dkg (166.84.xx.xx) on Mon 19 Feb 2007 at 01:05
[ Send Message | View dkg's Scratchpad | View Weblogs ]
Reading RFC 2616 (HTTP/1.1), i find that HTTP uses CRLF line feeds as well, just like SMTP. i'm not sure how i've gotten away with not knowing that all these years. anyway, here's the updated version of hsrv, for folks who might want it.
# make a TCP connection to a standard service, using readline to track your history:                                                                           
# example usage:                                                                
#   hsrv http www.example.com                                                   
# you can specify an alternate port by appending it:                            
#   hsrv smtp mailserver.example.com 2525                                       
# FIXME: which services	besides smtp and http need the crnl option?             
function hsrv() {
    [ -d "$HOME/.histories" ] || mkdir "$HOME/.histories"
    local extraopts=""
    ( [ "smtp" == "$1" ] || [ "http" == "$1" ] ) && extraopts=",crnl"
    socat "READLINE,history=$HOME/.histories/$1$extraopts" "TCP4:$2:${3:-$1}"
}

[ Parent | Reply to this comment ]

Posted by ajt (195.112.xx.xx) on Wed 14 Feb 2007 at 20:13
[ Send Message | View Weblogs ]
Not until you suggested it. Looks very cool and most helpful, thanks for the tip.

I can see the utility of your suggestion in most cases. I tend to use my h alias to see several commands at the same time to see variations all at once, but this is probably the minority of cases.

Our Red Hat and AIX boxen at work are a bit more antique and probably won't support this.

--
"It's Not Magic, It's Work"
Adam

[ Parent | Reply to this comment ]

Posted by dkg (216.254.xx.xx) on Wed 14 Feb 2007 at 21:06
[ Send Message | View dkg's Scratchpad | View Weblogs ]
give it a try and report back what you find. Since i posted that, i keep realizing i use reverse-i-search in more places without even noticing it. It's part of my lizard brain now, i guess.

It works in psql, sqlite, and mysql (command-line db clients for postgres, sqlite, and MySQL, respectively), octave, trac-admin, and probably many other places.

[ Parent | Reply to this comment ]

Posted by daemon (155.232.xx.xx) on Thu 15 Feb 2007 at 17:25
[ Send Message | View Weblogs ]
I also use it all over the place, and it'll work with any app that has readline support linked in (it's a readline feature, rather than a BASH feature specifically).

For those apps that _don't_ have a similar feature, but you'd like to have it, there's a tool called "rlwrap" that is supposed to do just that -- I haven't tried it yet as it but it's on my todo list.

Cheers.

[ Parent | Reply to this comment ]

Posted by eric (194.2.xx.xx) on Fri 23 Feb 2007 at 15:20
[ Send Message | View Weblogs ]
Great! I like it.
I remember having seen this tip before but I forgot it...

:eric:
http://blog.sietch-tabr.com

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search