Sponsored Content
Top Forums Shell Programming and Scripting Conditionally replace nth argument of every function call Post 302908567 by Chubler_XL on Tuesday 8th of July 2014 06:27:45 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Text::Glob(3)						User Contributed Perl Documentation					     Text::Glob(3)

NAME
Text::Glob - match globbing patterns against text SYNOPSIS
use Text::Glob qw( match_glob glob_to_regex ); print "matched " if match_glob( "foo.*", "foo.bar" ); # prints foo.bar and foo.baz my $regex = glob_to_regex( "foo.*" ); for ( qw( foo.bar foo.baz foo bar ) ) { print "matched: $_ " if /$regex/; } DESCRIPTION
Text::Glob implements glob(3) style matching that can be used to match against text, rather than fetching names from a filesystem. If you want to do full file globbing use the File::Glob module instead. Routines match_glob( $glob, @things_to_test ) Returns the list of things which match the glob from the source list. glob_to_regex( $glob ) Returns a compiled regex which is the equivalent of the globbing pattern. glob_to_regex_string( $glob ) Returns a regex string which is the equivalent of the globbing pattern. SYNTAX
The following metacharacters and rules are respected. "*" - match zero or more characters "a*" matches "a", "aa", "aaaa" and many many more. "?" - match exactly one character "a?" matches "aa", but not "a", or "aaa" Character sets/ranges "example.[ch]" matches "example.c" and "example.h" "demo.[a-c]" matches "demo.a", "demo.b", and "demo.c" alternation "example.{foo,bar,baz}" matches "example.foo", "example.bar", and "example.baz" leading . must be explictly matched "*.foo" does not match ".bar.foo". For this you must either specify the leading . in the glob pattern (".*.foo"), or set $Text::Glob::strict_leading_dot to a false value while compiling the regex. "*" and "?" do not match / "*.foo" does not match "bar/baz.foo". For this you must either explicitly match the / in the glob ("*/*.foo"), or set $Text::Glob::strict_wildcard_slash to a false value with compiling the regex. BUGS
The code uses qr// to produce compiled regexes, therefore this module requires perl version 5.005_03 or newer. AUTHOR
Richard Clamp <richardc@unixbeard.net> COPYRIGHT
Copyright (C) 2002, 2003, 2006, 2007 Richard Clamp. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
File::Glob, glob(3) perl v5.16.3 2011-02-22 Text::Glob(3)
All times are GMT -4. The time now is 07:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy