Hey I have this script that replaces occurances of a pattern inside files.
gawk -v RS='<[^>. ]+>' '{ ORS="" }
RT {
switch (RT)
{
case /E:/:
if (!(RT in events))
events[RT] = eventCount++
name=RT
sub(/<E:/, "", name)
sub(/>/, "", name)
ORS=name ", " events[RT]
break
...
}
}
{
print $0 > (FILENAME ".c")
}' $files
I need to modify the script in a way so that it only replaces the patterns if they are found inside the function definitions of the functions that I specify in a variable. For example:
gawk -v RS='<[^>. ]+>' -v FUNCS='a(void) b(void) foobar(void)''{ ORS="" }
RT {
#if inside of one of FUNCS
switch (RT)
{
case /E:/:
if (!(RT in events))
events[RT] = eventCount++
name=RT
sub(/<E:/, "", name)
sub(/>/, "", name)
ORS=name ", " events[RT]
break
...
}
}
{
print $0 > (FILENAME ".c")
}' foo.c bar.c
foo.c:
void a(void)
{
<E:X>
}
void b(void)
{
<E:Y>
}
void barfoo(void)
{
<E:Z>
}
bar.c:
void c(void)
{
<E:A>
}
void foo_bar(void)
{
<A:B>
}
after running the script the files should look like this:
foo.c:
void a(void)
{
0
}
void b(void)
{
1
}
void barfoo(void)
{
<E:Z>
}
bar.c:
void c(void)
{
<E:A>
}
void foo_bar(void)
{
2
}
Edit:
I have a problem with the current solution because it doesnt work in some functions. The example code I am testing against:
test.c
void foobar_(void)
{
<E:X>
}
void foobar(void)
{
<E:X>
}
test.c.tmp
void foobar_(void)
{
<E:X>
}
void foobar(void)
{
<0>
}
and the code that I run:
awk -v funcs='foobar(void) foobar_(void)' '
BEGIN {
split(funcs,tmp)
for (i in tmp) {
fnames[tmp[i]]
}
}
/^[[:space:]]*[[:alnum:]_]+[[:space:]]*[[:alnum:]]+([^)]*)/ {
inFunc = ($NF in fnames ? 1 : 0)
}
{
head = ""
tail = $0
while ( inFunc && match(tail,/<E:[^>]+>/) ) {
tgt = substr(tail,RSTART+1,RLENGTH-2)
if ( !(tgt in map) ) {
map[tgt] = cnt++
}
head = head substr(tail,1,RSTART) map[tgt]
tail = substr(tail,RSTART+RLENGTH-1)
}
$0 = head tail
}
{
print $0 > (FILENAME ".tmp")
}' $module_files
question from:
https://stackoverflow.com/questions/65935347/only-replace-patterns-if-they-are-found-inside-the-function-definitions-of-funct