Let's suppose you want to apply some AWK program to your selected text in Notepad++.
The first thing you require is NppExec plugin ;)
The second thing you require is an AWK executable, for example, "awk95.exe" or "gawk.exe".
When you want to apply some AWK program to your selected text, this AWK program must exists, isn't it? So, create a file "do.awk" in your Notepad++'s folder. The full path to this file is "$(NPP_DIRECTORY)\do.awk".
OK, the file is created, now you need some AWK program. Open the "do.awk" in Notepad++ and type your program, for example:
{ print $0 }
This simple AWK program will print each record (i.e. text line) from the input file given.
Now, open another file in Notepad++ and select some text (few lines, for example).
Now you have the selected text and a tool (awk95.exe or gawk.exe) you want to apply to this selected text.
The only thing remaining is NppExec's script which will allow you to do what you want.
Press F6 (or Plugins -> NppExec -> Execute NppExec Script...) and type:
// full path to AWK executable set local AWK_EXE = C:\tools\awk\awk95.exe // this temporary file name will be used set local TEMP_FILE = $(SYS.TEMP)\npp_sel.txt // save current selection as ANSI text file SEL_SAVETO $(TEMP_FILE) :a // run do.awk for this file "$(AWK_EXE)" -f "$(NPP_DIRECTORY)\do.awk" "$(TEMP_FILE)"
Save this script as "do.awk on selected text" or whatever you want.
Press OK.
You will see the AWK's output in the NppExec's Console window. You can copy it from there with Ctrl+C.
As you understand, such approach can be used for any command-line external tool such as grep, php and so on.
Good luck!
P.S. By the way, if you have nothing selected and you want to select a word under the caret, the following script can be used:
// get current position... SCI_SENDMSG SCI_GETCURRENTPOS set local pos = $(MSG_RESULT) // get start of a word near the pos... SCI_SENDMSG SCI_WORDSTARTPOSITION $(pos) 1 set local wordStart = $(MSG_RESULT) // get end of a word near the pos... SCI_SENDMSG SCI_WORDENDPOSITION $(pos) 1 set local wordEnd = $(MSG_RESULT) // set selection SCI_SENDMSG SCI_SETSEL $(wordStart) $(wordEnd)
If you combine this script with the previous one, you will be able to apply an external tool to a word under the caret. (I.e. first it will select a word under the caret, and then will apply an external tool to that selected word.) Also consider ability to use $(CURRENT_WORD) instead of SEL_SAVETO in this case (see [4.6.3]).
See also: Processing & replacing the selection [4.6.3]; Modify selected text and save to file [4.6.17].