Conditionally replace nth argument of every function call


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditionally replace nth argument of every function call
# 1  
Old 07-07-2014
Conditionally replace nth argument of every function call

I have very large perl source code file and I want to replace every occurrence function say foo,The function foo has some arguments and I want to replace 2nd argument,the current argument is hex integer and i want to replace it to equivalent string.Also I want to replace function name foo with bar_new e.g.
Code:
foo(a,0x1,b,c,d) should be replaced with bar_new(a,$some_obj->one,b,c,d)
foo(a,0x2,b,c,d) should be replaced with bar_new(a,$some_obj->two,b,c,d)
 .....
 .....
foo(a,0x9,b,c,d) should be replaced with bar_new(a,$some_obj->nine,b,c,d)

So basically, I want to replace 2nd column with equivalent string from look up. How can i do that?
# 2  
Old 07-07-2014
I do not understand what you are trying to do.

Replace with equivalent string from look up in what? Can we just make up random strings???

Please show us ALL of your inputs, a sample input file, and a corresponding sample output file.

What code have you tried?
# 3  
Old 07-08-2014
Hi Don,
The function foo was perl API to do some task and Now as per new standard APIs are changed.
The foo API is renamed to bar_new. New API accepts string equivalent of previous integer argument. So I have to change the code as per new API. The look up is as below
  • argument string equivalent
  • 0x1 $some_obj->one
  • 0x2 $some_obj->two
  • ...
  • ...
  • 0xf $some_obj->fifteen

I have tried sed to replace every occurrence of function foo with bar_new,but I am unable to replace argument conditionally using the lookup table

---------- Post updated at 10:55 PM ---------- Previous update was at 10:49 PM ----------

perl code looks like:

Code:
#!/usr/bin/perl

# Function definitions
## Some perl code
# Function call
$num = foo($t, 0x2, $my_x,$my_y);
#some perl code
$num1 = foo($m, 0x9, $my_u,$my_v);
#some perl code
$num1 = foo($e, 0x5, $my_a,$my_b);

---------- Post updated at 10:57 PM ---------- Previous update was at 10:55 PM ----------

sample output
Code:
#!/usr/bin/perl

# Function definitions
## Some perl code
# Function call
$num = bar_new($t, $some_obj->two, $my_x,$my_y);
#some perl code
$num1 = bar_new($m, $some_obj->nine, $my_u,$my_v);
#some perl code
$num1 = bar_new($e, $some_obj->five, $my_a,$my_b);

# 4  
Old 07-08-2014
If you're happy with an awk solution, try
Code:
awk -F, 'BEGIN          {split ("one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen", NUM)}
         /foo/          {sub (/foo/,"bar_new"); $2="$some_obj->" NUM[sprintf("%d",$2)]}
         1
        '  OFS="," file
bar_new(a,$some_obj->one,b,c,d)
bar_new(a,$some_obj->two,b,c,d)
bar_new(a,$some_obj->nine,b,c,d)
bar_new(a,$some_obj->fifteen,b,c,d)

# 5  
Old 07-08-2014
Always be very wary of automated code changes, keep backups!

I been having a play with this problem and managed to add support for multi-line function calls, quoted strings and sub function calls. This version can work with input as complex as this:

Code:
  $num = foo("Testing, time", 0x02, $my_x, $my_y));
  $num = "Nothing to change here";

  foo(
      bar(test,this),
      0x07,
      "done");

Note I replaced the hex conversion to simple from and to lists for more flexability. You can just change your FROM and TO lists as you desire:

Code:
awk '
function replace_param(string,num,inq,inb,fval,out) {
   fld=1
   for(i=1;i<=length(string);i++) {
      chr=substr(string,i,1)
      if (!inq && chr  == "(") inb++
      if (!inq && inb && chr  == ")") inb--
      if (chr  == "\"") inq=!inq
      if (!inb && !inq && chr  == ",") {
          if(fld==num) out = out "$some_obj->" REPL[tolower(fval)]
          out = out ","
          fld++;
          while(substr(string,++i,1)~"[[:blank:]\\n]") out = out substr(string,i,1);
      }
      if (fld==num && !inq && !inb) fval=fval substr(string,i,1);
      if (fld!=num) out=out substr(string,i,1);
   }
   return out
}

BEGIN {
    RS=";"
    split ("one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen", TO, ",")
    split ("0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f", FROM, ",")
    for(i in FROM) REPL[FROM[i]]=TO[i]
}
$0 ~"[^[:alnum:]_]foo[^[:alnum:]_]*\\(" {
    s=$0
    while(match(s, "[^[:alnum:]_]foo[^[:alnum:]_]*\\(")) {
        s=substr(s, 1, RSTART) "bar_new(" replace_param(substr(s,RSTART+RLENGTH), 2)
    }
    $0 = s
}
{ printf "%s", $0 (length>1? RS:"")}' infile

Result:

Code:
  $num = bar_new("Testing, time", $some_obj->two, $my_x, $my_y));
  $num = "Nothing to change here";

  bar_new(
      bar(test,this),
      $some_obj->seven,
      "done");


Last edited by Chubler_XL; 07-08-2014 at 08:52 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Conditionally replace field content

Hello I am trying to conditionally replace field content in $2 of input file if pattern is found in $4 of the same tab-separated input file. Currently, $2 is empty. I am trying (with no success): awk -F "\t" 'BEGIN {FS="\t"}{ if ($4=="NO") $2=$2"NO"; print $0}' in > out (2 Replies)
Discussion started by: dovah
2 Replies

2. Shell Programming and Scripting

Replace nth to nth character?

Hi I got the following problem and I wonder if some could please help me out? I'd like to replace character 8 - 16 , 16 - 24 cat file ... (2 Replies)
Discussion started by: stinkefisch
2 Replies

3. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

4. Shell Programming and Scripting

How to conditionally replace a pattern?

Hi, How to replace only the function calls with a new name and skip the function definition and declarations. consider the following code. There are 2 functions defined here returnint and returnvoid. I need to replace returnint with giveint and returnvoid with givevoid only in the function... (2 Replies)
Discussion started by: i.srini89
2 Replies

5. UNIX for Dummies Questions & Answers

How to conditionally replace a pattern?

Hi, How to replace only the function calls with a new name and skip the function definition and declarations. consider the following code. There are 2 functions defined here returnint and returnvoid. I need to replace returnint with giveint and returnvoid with givevoid only in the function... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. Shell Programming and Scripting

How to replace a text in a file conditionally?

I have got about 100 ascii files and I want replace some variable with a new one on an HP-UX system. But I want to put a line of comments before the change. I want to make file1 to file2. I am explaining below. file1: line1 line2 export QNAME=ABC line4 line5 file2: line1 line2 #... (3 Replies)
Discussion started by: asutoshch
3 Replies

7. Shell Programming and Scripting

pass function as argument to a function

I have the following code : function1 () { print "January" } function2() { case $1 in January) print "Dzisiaj mamy styczen" ;; *) ;; } main() { (1 Reply)
Discussion started by: presul
1 Replies

8. Shell Programming and Scripting

Function call with argument doubt

Hi all, I am having a problem with user defined function call. I am new into the concept of shell script UDFs. My function is: iterate_directory() { cd $1 k=0 for i in * do if then ARR=${i} fi done echo ${ARR } } (4 Replies)
Discussion started by: canishk
4 Replies

9. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

10. UNIX for Advanced & Expert Users

how to use exceptfds argument in select system call

Hi, Can any one tell me how to use the fourth argument of select system call.I saw example "port forwarding" on the net,but it was too complex for me to understand.Can any one explain me about the usage of exceptfds argument of select system call with simple example. Thanks. (2 Replies)
Discussion started by: bvijaya
2 Replies
Login or Register to Ask a Question