[main]  

ModifyFiles

Documentation

Usage

    ModifyFiles OPTIONS ACTION FILES
    ModifyFiles -S OPTIONS ACTIONARGS -- FILES

Description

    Modify FILES by piping each file of FILES through ACTION in a 
    temporary file before moving back the file to the original location.

Options

    -d, --diff              do a diff to show changes
    -E, --export-filename   export current filename to the environment 
                            variable 'MF_FILENAME'
    --help                  display short help text
    -i, --interactive       prompt before applying changes
    -l, --less              call 'less' to show the resulting file before
                            making any changes
    --man, --manual         display complete help with examples etc.
    -n, --dry-run           don't actually modify files
    -p, --keep-permissions  keep file permissions
    -q, --quiet             don't print names of modified files
    -S                      use syntax ACTIONARGS -- FILES rather than put 
                            the whole ACTION construct into a single argument
    -t, --keep-mod-time     keep the modification time of the file
    -v, --verbose           be a bit verbose
    -V, --version           output version information and exit
    -y, --modify-symlinks   modify destination/target of symbolic links
                            instead of the contents of files
    -z, --gzip              (experimental:) assume FILES are compressed 
                            with 'gzip'

Notes

    1 ACTION: input must come from stdin, output must go to stdout
    2 ACTION can be a piping construct
    3 If ACTION fails, no changes will be made
    4 If the ACTIONARGS syntax is used:
      - arguments not prefixed with "@" are put into quotes
      - arguments prefixed with "@" are not put into quotes, and the 
        "@" prefix will be removed from the argument
    5 If the resulting file is identical with the original file, nothing 
      will be done
    6 If the option '--gzip' is used, the filenames must have the extension 
      ".gz", because some needed programs (e.g. zdiff) may require this.

Examples

    Remove the CR (carriage return) character (0xD, "\r") in files *.sh 
    (interactive confirmation, keep file modification times):
        ModifyFiles -S -i --keep-mod-time tr -d "\r" -- *.sh

    Change tabulator width of 5 to a tab width of 4 in files *.c:
        ModifyFiles "expand -t 5 | unexpand -t 4" *.c

    Same as above, but using the -S syntax:
        ModifyFiles -S expand -t 5 "@\|" unexpand -t 4 -- *.c

    Replace in *.c*, *.h files all strings "<common.h>" with 
    "<lib/common.h>", displaying the differences and using interactive 
    confirmation:
        ModifyFiles -d -i -v "sed 's,<common\\.h>,<lib/common.h>,g'" \
            *.c* *.h

    Replace in files *.c* *.h all words "int" with "long" using perl's
    regexp replacement (showing a diff, interactive)
        ModifyFiles -i -d "perl -p -e 's/\\bint\\b/long/g'" *.c* *.h

    Replace in files *.txt all "-chars with "'" (display result using 
    'less', ask user for confirmation)
        ModifyFiles --less -i -v --  "sed 's,\",'\'',g'" *.txt

    Add include guards to files *.h (with confirmation):
        ModifyFiles --export-filename -i --less \
            ' Define=_INCLUDED_`echo "$MF_FILENAME" \
                | sed -e "s,.*/,,g" -e "s,\.,_,g"` ;
            echo -e "#ifndef $Define\n#define $Define\n" ;
                cat - ; echo -e "\n#endif" ' \
            *.h

    Modify targets of symlinks from /mnt/local/hda9/* to 
    /mnt/local/hda10/* (don't actually apply changes):
        ModifyFiles -y -d -i -n \
            'sed "s,^/mnt/local/hda9/,/mnt/local/hda10/,"' \
            `find . -type l`

Bugs

    - only tested with bash
    - (more: see lines in script file marked with BUG)

See Also

    man perlrun, see option "-i" (for in-place operation);
    RenameFiles

Source Code

License: GPL, Version 2 (or later version).

  [main]  
DaicasWeb v.1.50.0102  //   Daniel Käps  //   March 31, 2005  //   Impressum / Imprint 
http://www.daicas.net/programming/ModifyFiles.html