What is it

Contextual is a program for inserting a line... ...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:

    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
    

    Download

    Download the contextual script here.


    Hits: 6150
    Timestamp: 2024-04-18 10:07:42 PDT

    Back to Dan's tech tidbits

    You can e-mail the author with questions or comments: