How to conditionally replace a pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to conditionally replace a pattern?
# 1  
Old 04-30-2012
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 calls.
Code:
1  static int returnint(int num1);
2  static void returnvoid(int num1);
3  void main()
4  {
5     int num1;
6     num1=returnint(3);
7     returnvoid(2);
8     returnvoid(5);
9   }
10  static int returnint(int num1)
11  {
12       returnvoid(5);
13       return 12;
14   }
15   static void returnvoid(int num1)
16   {
17        return;
18   }

function calls are present in line numbers 6,7,8 and 12. so in all these lines, the existing function is to be replaced with the new names given.
funcion declarations and definitions present at lines 1,2,10 and 15 are to be skipped and the original function names are to be retained.
sample output:
Code:
1  static int returnint(int num1);
2  static void returnvoid(int num1);
3  void main()
4  {
5     int num1;
6     num1=giveint(3);
7     givevoid(2);
8     givevoid(5);
9   }
10  static int returnint(int num1)
11  {
12       givevoid(5);
13       return 12;
14   }
15   static void returnvoid(int num1)
16   {
17        return;
18   }

Thanks,
Srini
# 2  
Old 04-30-2012
Code:
 
$ nawk '!/static.*return/{if($0!~/return[\; ]/)sub("return","get");print}/static.*return/' test.txt
static int returnint(int num1);
static void returnvoid(int num1);
void main()
{
int num1;
num1=getint(3);
getvoid(2);
getvoid(5);
}
static int returnint(int num1)
{
  getvoid(5);
  return 12;
}
static void returnvoid(int num1)
{
   return;
}

# 3  
Old 04-30-2012
There is one logic i can think of. both the function definition and declaration have the return types(int or void or long) in them along with the function name.
So can we form an array of all such possible return types and when the returntype present in the array is there along with function, it is skipped else it is changed with the new name into the same file?
i could not arrive at any code for this logic. Smilie

---------- Post updated at 04:44 PM ---------- Previous update was at 04:38 PM ----------

@itkamaraj : Thanks for the reply.
But it is NOT the convention that the function name has to start with returnABC etc, it can be anything like validateABC or getTheValOfABC etc. Also static is optional in both function definition and declaration. Can u please help by checking my previous comment (using the array of return types one).

Thanks,
Srini
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

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: pravint
4 Replies

4. Shell Programming and Scripting

Linux: replace pattern with pattern

Hi, I am a rookie of Linux. I have a problem on how can I replace a certain pattern in Linux with nothing. Can anyone help me?:( sample.txt: <binding>App189 ABC SampleMachine1 ABC XXX YYY ZZZ </binding> <binding>App190 ABC SampleMachine2 ABC XXX YYY ZZZ </binding> <binding>App191... (3 Replies)
Discussion started by: dirkaulo
3 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

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

7. Shell Programming and Scripting

replace pattern after the first pattern match

I need this. aaa OOOOO bbb ccc OOOOO ddd fff ggg OOOOO iii OOOOO I need all OOOOO replaced with PPPPP, but only change after the pattern ggg. So the first two OOOOO should not be changed. OUTPUT should be :- aaa (2 Replies)
Discussion started by: anilcliff
2 Replies

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

9. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question