CONTEXT
— Search for words in
English text and display them in context.
Syntax:
CONTEXT
/A:
attribs /C:
n /CP:
n /F:
n /H:
n /K:
n /P /N /S /V /W:
base /X:
word /Y:
word
filename…
/A: attribs | attributes mask; valid flags are -ACEHIORS |
/C: n | specifies the number of sentences of context to display, before and after |
/CP: n | interpret non-Unicode input text using code page n |
/F: n | specifies the format of the input text; n is one of: |
0 — best guess (default) | |
1 — unformatted (line breaks are used only to end paragraphs) | |
2 — prewrapped (line breaks are used to wrap text) | |
3 — unformatted, with blank lines between paragraphs | |
/H: n | set highlight colors for matching words |
/K: n | output columns for word-wrap |
/P | page output |
/N | disable features |
/S | search in subdirectories for matching filenames |
/V | verbose; report counts of found items after each file and at the end |
/W: base | search for forms of a word |
/W:" base base…" | search for a series of word forms |
/X: word | search for an exact word |
/X:" word word…" | search for a series of exact words |
/Y: word | search for words that sound like word |
… | Range options are also supported. |
CONTEXT
can read from disk files or
from a pipe. If you want to pipe to CONTEXT
, remember that pipes
open a new shell. To pipe to a plugin command, you must either ensure that the
plugin is loaded in the transient shell, e.g. by installing the
.DLL file in the shell’s PlugIns
directory; or else use temporary files or an in-process pipe.
You may specify more than one filename;
wildcards and directory aliases are supported. You can search recursively into
subdirectories for matching files with /S
. @File lists and
internet files are supported. You may also specify CLIP:
to
search for words on the clipboard.
Note: This command was created specifically to search through English text. I make many Anglocentric assumptions about what constitutes a ‘word’, a ‘sentence’, a ‘paragraph’, ‘forms’ of a word, and so on. These assumptions are probably not useful for any other language.
Word search: /W:
base
searches for forms of a word; this will probably be your most frequently-used
option. Specify the base form of a word, and CONTEXT
will attempt
to match variations of it. For example, /W:DOG
will match dog,
dogs, dog’s, doggy, and even doggedly.
A word in the input text is considered a ‘form’ of the specified
base word if (1) the beginning matches for the entire
length of base, except that a final Y at
the end of the base word will match an I in
the word from the text; and (2) the remainder of the word does not contain more
than one vowel other than Y. Case is not significant, and most common
accents are ignored; /W:garcon
will match garçon,
/W:"deja vu"
will match Déjà vu, and so on.
If a word from the input text contains a hyphen, the /W:
search
will also look for the specified base word to either
side of the hyphen; /W:LEVEL
will match level-headed,
sub-level, and even poorly-levelled.
Word series: You can search for a series of words with
/W:"
base base…"
.
To match, a series of words must appear within the same sentence in the input
text; a word series cannot span the end of a sentence. Matching words must be
consecutive, and may be separated by spaces, tabs, or other punctuation.
CONTEXT
will check for forms of each base word as above,
but will not look for the base within hyphenated
words. For instance, /W:"LITTLE OLD LADY"
will match little,
old ladies.
Exact-word search: /X:
word
searches for a word without checking for variant forms. /X:
does
not look for the specified word within hyphenated
words. Case and accents are still ignored. You can search for a series of
exact words with /X:"
word word…"
.
Sound-alike search: /Y:
word searches
for words which sound similar to the specified word.
The comparison uses a Metaphone-like algorithm to guess at a word’s
pronunciation. (This type of search does not support word series.)
Surrounding context: By default, CONTEXT
displays one sentence before, and one sentence after, each sentence containing any
of the specified search words. You can adjust this value with
/C:
n; legal values are 0 to 15. Note
that you may see more than 2n sentences between
found words that are close together; CONTEXT
will display a little extra
text rather than introduce a very short break. You may also see fewer than
n sentences near the start or the end of a file.
Highlighting: If
CONTEXT
’s output is to the screen (i.e. stdout is
not redirected), text which matches your search words will be highlighted in a
different color. By default, CONTEXT
picks a highlight color which
contrasts with the current console colors. You can specify your own highlight
color either with the option /H:
n, or
by setting an environment variable named HIGHLIGHT
. Either way,
the value should be a decimal number from 1 to 254, or a hexadecimal value from
0x01 to 0xFE. The high four bits set the background color, and the low four
bits set the foreground color; the two values must be different. The command-line
option takes precedence over the environment variable. You can disable highlighting
with /NC
. Text is not highlighted if the commands’s output
is redirected.
Reports: If /V
is specified,
CONTEXT
will also report the number of times each search word
was found within a file. If more than one file is processed it will also show
a final report for all files, giving the number of times each search word was
found in total, and in how many files.
Text encoding: CONTEXT
automatically
detects Unicode text files. If the file is not Unicode, the command has no way
of detecting the character encoding; the default Windows code page is assumed.
You can specify a different code page for non-Unicode text files with
/CP:
n. Most single-byte (i.e.,
alphabetic) code pages are supported, but multibyte
code pages (Chinese, Japanese, Korean) are not. This option only affects
non-Unicode files.
Text format: Text files use line-break characters
in different ways. In some files, line break characters are used only to mark
where a line end should occur: the end of a paragraph. In other files,
line breaks are used to wrap text to some desired width. You can use
/F:
n to tell CONTEXT
how to handle line breaks. /F:1
indicates that the text is unformatted,
with line breaks only at the ends of paragraphs. CONTEXT
will
honor all line breaks, and add an extra blank line after each paragraph.
/F:2
means that the input text is prewrapped, having line
breaks within paragraphs and even within sentences. CONTEXT
will
skip single line breaks, honoring only sequences of two or more in a row. /F:3
is also for unformatted text and acts like /F:1
, but does not insert a
blank line after each paragraph. If you specify /F:0
or do not
specify any /F:
n, CONTEXT
will attempt to guess how the input text is formatted. (Guessing is not reliable
when there isn’t much input text.)
Word wrap: Text output by CONTEXT
will be word-wrapped. If output is to the screen, it will be wrapped to the
screen width. If output has been redirected, the default width is 100 columns.
You can set a different width using the /K:
n
option; the value must be between 40 and 512.
Disabling features:
/N
disables features:
/NB | do not write a Byte Order Mark |
/NC | disable highlight |
/ND | do not search into hidden directories; only useful with /S |
/NF | suppress the file-not-found error |
/NJ | do not search into junctions; only useful with /S |
/NZ | do not search into system directories; only useful with /S |
You can combine these, e.g. /NDJ
.
C:\> context http://www.gutenberg.org/files/11/11-0.txt /w:paint
File "D:\download\pg11.txt" :
CHAPTER VIII. The Queen's Croquet-Ground
A large rose-tree stood near the entrance of the garden: the roses growing on it
were white, but there were three gardeners at it, busily painting
them red. Alice thought this a very curious thing, and she went nearer to watch
them, and just as she came up to them she heard one of them say, 'Look out now,
Five! Don't go splashing paint over me like that!'
'I couldn't help it,' said Five, in a sulky tone; 'Seven jogged my elbow.'
* * *
Seven flung down his brush, and had just begun 'Well, of all the unjust things--'
when his eye chanced to fall upon Alice, as she stood watching them, and he
checked himself suddenly: the others looked round also, and all of them bowed low.
'Would you tell me,' said Alice, a little timidly, 'why you are painting
those roses?'
Five and Seven said nothing, but looked at Two.
C:\>