...occurrences of a given shell regular expression (also known as a
"glob expression"). A "shell regular expression" means you use "*" to
match 0 or more occurrences of any character, and "[a-z]" should match 1
occurrence of any lower case letter in the Roman alphabet, and of course
"foobar" will match "foobar" .
The program is useful in scenarios where you cannot just add lines at
the end of a file (or more generally, a particular line number), because
you could conceivably end up with the lines of the file ordered
incorrectly. For a file that doesn't care about order, you're probably
better off with "echo foo >> /dir/file" or (in the DCS autoinstall
case) using "add-line".
Implementation language
Despite the implementation language (POSIX shell), which often yields
implementations that are slow and tend to munge white space, I believe
that contextual should preserve whitespace well, and should run
efficiently because it uses no external programs (except in the very
most archaic versions of bourne shell, where things that are builtins
now, used to be external). Note that -t may have newlines in it, but -p
may not - a shell regular expression with a newline in it, I expect,
will never match anything.
Portability
I anticipate that contextual will run with any POSIX shell (sh, ksh,
dtksh, pdksh, bash) that supports shell functions and "exec > file" as well
as "exec < file".
License
This software is owned by The university of California, Irvine,
and is not distributed under
any version of the GPL. GPL is a fine series of licenses, but the owners of the software need it to be distributed under these terms.
Usage
Usage is like:
seki-strombrg> contextual
-p must be specified exactly once
usage: usage follows
-p pattern specify the shell pattern, before or after which the text will be added
-a perform operation after the pattern
-b perform operation before the pattern
-i perform an insert
-r perform a removal (not yet implemented)
-t text the text to add
-f add text only before/after the -first- occurrence of the pattern
-F filename file to operate on. Default is to read from stdin and write to stdout
-d output some debugging information
You must always specify both -p and -t
You must always specify either -a or -b but not both
You must always specify either -i or -r but not both
An example
seki-strombrg> cat /tmp/testfile
1
2
3
foobie bletch
4
line with leading whitespace
5
6
test line with whitespace
test line with tabs
foobie bletch
seki-strombrg> contextual -p 'foobie bletch' -f -b -i -t 'line to add with whitespace' < /tmp/testfile
1
2
3
line to add with whitespace
foobie bletch
4
line with leading whitespace
5
6
test line with whitespace
test line with tabs
foobie bletch