Run Command Tutorial

 

Table of Contents

Parsing Command Output

Introduction

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

Parsing with a Regular Expression

To parse command output, you must specify a regular expression. Each line of output from the command sent to the Command Output tab 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

Using "Find in Files"

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.

  1. On the Projects tab, 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.
  2. Position the cursor over the word "frogs".
  3. Select Tools|Run Command.
  4. 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.

  5. Select the Add to Toolbox check box to save this command.
  6. 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.
  7. Select the Parse output with check box and enter:
      ^(?P<file>.+?):(?P<line>\d+):(?P<content>.*)$
    as the regular expression with which to parse.
  8. Select the Show parsed output as a list check box.
  9. Click the Run button to run the command. The Interpolation Query dialog box will be displayed.

    View Example

  10. Click OK to run findstr. A list of all occurrences of "frogs" in the files of your Run Command tutorial project is displayed on the Command Output tab.

    View Example

  11. Double click on a parsed result to jump to a specific file and line.
  12. Click the button to toggle back to the Command Output tab. View Example
  13. You can double click on lines in the raw output view to jump to that file and line, as well

Top
Top Previous Home