LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2014, 11:45 AM   #1
pravint
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Rep: Reputation: Disabled
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?
 
Old 07-07-2014, 12:58 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by pravint View Post
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.
 
Old 07-08-2014, 03:44 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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_
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to append a numeral to nth column of a line containing a particular word bal_nair Programming 10 03-12-2014 10:38 AM
Replace nth line after the pattern match sras Solaris / OpenSolaris 9 08-07-2013 06:12 AM
Get nth occurrence of two strings and replace it Mann_engg Linux - Newbie 2 05-30-2013 05:56 PM
[SOLVED] replace the nth column date as MM/DD/YYYY anshaa Linux - Newbie 1 08-13-2012 04:25 AM
Perl Search and Replace XML tags conditionally rammyp_1979 Programming 15 10-22-2010 09:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration