Conditionally replace nth column of every function call
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
#!/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_
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.