Parsing Command Output
You can use the Run Command feature to specify a regular expression to parse
filename and line number information from lines of output. The parsed results are
displayed in a table, making it easy to quickly identify the desired file. You will
see how this works when you create a "Find in Files" command later in this section.
Top
To parse command output, you must specify a regular expression. Each line of
output from the command sent to the Command Output pane is also passed through
your regular expression. If the regular expression matches, an entry is
added to the list of parsed results.
Python's regular expression syntax is used to parse command output. Its
regular expression syntax is largely identical to Perl's except for one relevant
exception: named groups. Named groups are covered briefly in this tutorial because they are
important for effectively parsing command output. For a more thorough reference, visit
python.org.
Suppose you want to parse the following line of output generated by running grep:
hello.pl:5:print "Hello, frogs!\n";
Output lines are of the form:
<file>:<line>:<content>
An appropriate regular expression to match this line could be:
(.+?):(\d+):(.*)
However, when parsing this line, Komodo requires that you specify which group
(i.e., which parenthesized section) is a filename, which is a line number, and which
is content. This is where Python's named groups come in. Using Python's regular
expression syntax, you can write, for example, (?P<file>.+?)
instead of just (.+?) to assign a name to whatever matches ".+? ".
When parsing output from the Run Command, Komodo searches for the names file, line,
column and content to determine which part of a matched output line to put into
which field in its table of results. Note that because a column was not included in
this particular line of output, Komodo does not assign a column name.
So, in our example, we can extend our regular expression to:
(?P<file>.+?):(?P<line>\d+):(?P<content>.*)
When parsing the line above, Komodo will determine that "hello.pl "
is the file, "5 " is the line and "print
"Hello, frogs!\n"; " is the content. Click on the link below to see
the result highlighted in the parsed output.
View
Example
Top
Now you can put together everything you have learned about Komodo's Run Command system
to create a powerful "Find in Files" command for Komodo.
- In the Projects pane, double click the file
hello.pl .
This file will be opened in the Editor pane; a tab at the top of the
pane displays its name.
- Position the cursor over the word "frogs".
- Select Tools|Run Command.
- On Windows, enter the command:
findstr /s /n /c:"%(w:orask:Search Term)" "%(ask:File Pattern:*.*)"
Or on Linux enter the command:
find . -name "%(ask:File Pattern:*)" | xargs -l grep -nH "%(w:orask:Search Term)"
Note that findstr is a Windows command line utility that searches for strings in files.
- Select the Add to Toolbox check box to save this command.
- In the Start in field, enter:
"%(ask:Start Directory:%D) "
This specifies that, when the command is run, Komodo should prompt you for
the "Start Directory" using the directory of the current file (aka
%D ) as the default value.
- Select the Parse output with check box and enter:
^(?P<file>.+?):(?P<line>\d+):(?P<content>.*)$
as the regular expression with which to parse.
- Select the Show parsed output as a list check box.
- Click the Run button to run the command. The Interpolation Query dialog
box will be displayed.
View
Example
- Click OK to run findstr. A list of all
occurences of "frogs" in the files of your Run Command tutorial
project is displayed on the Command Output tab.
View
Example
- Double click on a parsed result to jump to a specific file and line.
- Click the
button to toggle back to the Command Output tab.
View Example
You can double click on lines in the raw output view to jump to that file and line, as well
Top
|