LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Conditionally replace nth column of every function call (https://www.linuxquestions.org/questions/programming-9/conditionally-replace-nth-column-of-every-function-call-4175510384/)

pravint 07-07-2014 11:45 AM

Conditionally replace nth column 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?

TB0ne 07-07-2014 12:58 PM

Quote:

Originally Posted by pravint (Post 5200045)
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?

Ok, there are MANY ways to do this. You can use the inline-editor part of sed "sed -i", to replace strings, but the better question is why you even need to do this? I've not seen ANY text-editors that don't have a basic search and replace function. The one in kdevelop can even accept wildcards, which would let you do the 0x1, 0x2 replacement.

That said, you can run "sed -i 's/foo/bar_new/g' <filename>" to replace the foo part. Then "sed -i s/a,.*,b/a,$some_object,b/g' <filename>". There are MANY easily-found sed tutorials you can reference.

bigearsbilly 07-08-2014 03:44 AM

Code:

#!/usr/bin/perl

use strict;

my $A = 'foo';
my $B = 'bar_new';
my $O = '$some_object';

my $paren_match = qr# *\(.*?\)#s;
my $expr = qr#(\b${A}${paren_match})#s;

my @nums = qw(zero one two three four five six sevn eight nine);

sub doit {

    local @ARGV = @_;
    warn "=>@_\n";
    local $/ = undef;
    local $" = ',';

    my $slurp = <ARGV>;
    warn "[$slurp]\n" ;

    my (@things) = $slurp =~ m#$expr#sg;
    #print "[@things]" if $verbose;

    foreach my $thing (@things) {
        warn "==> {$thing}\n" ;

        my ($list) = $thing =~ m#$A *\((.*)#s; 

        my @params = split ",", $list;
        warn "==> [@params]\n" ;
        warn ":$params[1]\n";
        my ($index) = $params[1] =~ m#0x(\d)#;
        $params[1] = "$O->$nums[$index]" if defined $index;
        $slurp =~ s#\Q$thing#$B(@params#;
    }
    print $slurp ;
}


doit $_ foreach  (@ARGV)

Okay it's a bit of a dog's breakfast, it's adapted from a C function replacer I have.

perl script.pl file ... will run it to look at.

perl -i_ script.pl file ... will replace and backup to file_


All times are GMT -5. The time now is 04:39 AM.