On Fri, 24 Jun 2005 16:34:27 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
comp.lang.c; comp.lang.awk added, fup there for the awk stuff:
<snip: OP wants to rename all FOO_fun<number> in a C source>
Why not just
#define FOO_fun1 othername
#define FOO_fun2 anothername
...
#define FOO_funN yet_anothername
If you don't what N will will be, and your build system allows, you can scan
the file and create the necessary #defines on the fly. For example, on Unix
you can do something like
grep -o -h 'FOO_fun[9-9]*' * <sourcefile \
| sort -u \
| awk '{N = substr($0,match($0,/[0-9]*$/)); print "#define " $0 " other" N}' \
> defines.h
and include defines.h
#if OFFTOPIC == mostly for clc
Presumably you meant [0-9], probably + instead of *, and not a
superfluous filename argument of *. -h is GNU-only, but not needed for
input from stdin (it sees no filename!) and -o doesn't exist on any
system/implementation I know about. awk can already handle what you
have in grep and sort, though your awk unnecessarily limits itself to
an identifier occuring at end of line which was not the OP's case; and
for nonUnix systems it is sometimes easier to get and install just awk
instead of a more extensive toolkit, and often easier to port an awk
script in a file rather than an xsh/ysh/CMD/somethingelse script.
awk <sourcefile -f blech.awk # containing, or quote appropriately
match($0,/FOO_fun[0-9]+/) && !done[$0=substr($0,RSTART,RLENGTH)]++ {
chg=$0; sub(/FOO/," BAR",chg); print "#define " $0 chg }
If FOO_bletch might occur within other names, add something like a
leading (^| |\t) or GNU-only \< or \y and/or similar trailing.
If you care about the order of the #define's can also do (reasonable)
sorting within awk, but OP's case doesn't need this.
The !done[]++ part, corresponding to your sort -u, isn't needed if the
input contains each function/name only once, which wasn't clear from
the OP; and isn't really needed even if it doesn't, since standard C
allows identical re-#define-itions, but that is sufficiently obscure
and rarely used to be confusing to readers and maintainers.
- David.Thompson1 at worldnet.att.net