Thursday, March 1, 2012

Seraching the text data on clipboard

Sometimes I need to use grep command on the certain text that I am looking at in an editor. Most of them do not have good grepping facility and even if they have there is not way to feed the results to some other process for further processing.
I ended up using a tool from nirsoft called nircmd to dump the clipboard contents in a text file and then use grep from cygwin. Here is a small 3 line script that I put in my bin folder along with the nircmd tool.

$ cat /cygdrive/c/cygwin/bin/clipGrep.sh
#!/bin/bash
MY_TMP_FILE=/cygdrive/c/tmp/UIUHUTGGUYGUhhjshjhasj_
rm $MY_TMP_FILE
nircmd.exe clipboard addfile `cygpath -w $MY_TMP_FILE`
cat $MY_TMP_FILE | grep $*
Now I can take any text on clip board and just execute this to filter what I want want. For example I selected all the text on Yahoo finance and took on clipboard (Ctrl-C) then searched for word "sell"

$ clipGrep.sh  -i sell
    Sell in March and Go Away?Matt Nesto

May not be a bad advice!

No comments: