------------- replace version: 2012-October-14 ------------- Usage: replace [OPTION(s)] OLDEXPR NEWEXPR FILE1 [ FILE2 FILE3 ... ] OLDEXPR and NEWEXPR should usually be in single quotes. Use perl regexp syntax, see man perlre. (yes, we need perl) (unescaped chars like "()^[]" have special meanings, cf. option -e). OPTION(s) must precede any other arguments. default is interactive mode, i.e. file is changed only after confirmation with 'y'. entering 'PROCEED' switches to non interactive mode (!) "-f" 'force': sets non-interactive mode (beware!). "-q" 'quiet': supress output of diffs (option is unset if "-n" is also given). "-Q" 'really quiet': output only if interaction is required "-n" sets dry run mode (diffs are shown, nothing is changed). "-c" print diffs with some lines of context "-d " produces a diff-file "-D " _only_ produces a diff-file "-e ''" auto-Escape chars from in OLDEXPR (slashes are always escaped; backslashes cannot be escaped) "-o " do not work inplace: use output file this option can only be used with exactly one file "-i" ignore case in OLDEXPR "-m" allow multiline match "-N" don't error if no files given (useful for scripts) Examples: replace old_func_name new_func_name *.cc ## you figure this out replace '([^ ]+) +$' '' *.txt ## remove trailing spaces replace -n -c oldstr newstr foo.txt ## check only, with context replace '\r' '\n' mac.txt ## apple line ends (^M) --> unix line ends (\n) replace '^ ' '\t' Makefile ## tabify Makefile replace '\t' ' ' $(find mytexts/ -name \*.txt) ## untabify files ## word boundaries: "n" --> "N", but not "and" --> "aNd": replace '\bn\b' 'N' *.cc ## ignore case: "Sand, sand, SAND" --> "sane": replace -i '\bsand\b' 'sane' *.txt replace '//(.*)$' '/\*$1\*/' *.cc ## C++ comments --> C comments replace '/\*(.*)\*/$' '//$1' *.c ## C comments --> C++ comments ## "sincos(arg1,arg2,arg3)" --> "sincos(arg3, arg2, arg1)": replace 'sincos\(([^,]+), *([^,]+), *([^\)]+)\)' \ 'sincos($3, $2, $1)' $(find . -name \*.cc) ## "name_foo" --> "new_foo", "name_bar" --> "new_bar" etc.: replace 'name_([a-z]+)' 'new_$1' *.cc ## replace "jjnote" by "myremark" in all *.cc files under fxt/: ## and produce patchfile fxt.dif: replace -d fxt.dif jjnote myremark $(find fxt/ -name \*.cc) ## ... undo changes by: patch -R -p0 < fxt.dif ## produce patchfile fxt.dif but do NOT change files inside fxt/: replace -f -D fxt.dif jjnote myremark $(find fxt/ -name \*.cc) ## ... apply these paches with: patch -p0 < fxt.dif ## replace "foo" --> "bar" in *.cc in the directory fxt/ ## but only in those files that contain "unixmagic": replace foo bar $(grep -l unixmagic $(find fxt/ -name \*.cc)) ## tricky: replace comments in postscript files: ## i.e. remove all '% blah' but not '%% blah' & don't touch magic ## (see man perlre 'zero-width negative lookbehind assertion') replace -fq '(?' '' $(find xml -name \*.xml) ## remove XML tag pairs with everything in between, ## even over multiple lines: replace -f -d d2.diff -m '.*?<\/remark>' '' $(find xml -name \*.xml) replace is online: at http://www.jjj.de/ ---------------- author: Joerg Arndt ( arndt (AT) jjj.de ) ----------------