sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi ,

I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows.

[root@ossvm24 mir_common]# diff mir_lex.c.modified mir_lex.c.orig
3209c3209
<                       if(yy_current_buffer -> yy_is_our_buffer == 0) {
---
> #ifdef YY_USES_REJECT
3212,3213c3212,3213
<                       }
<                       else {
---
> #else
>
3246,3247c3246,3247
<                       }
<               }
---
> #endif
>                       }

In the above mentioned diff , we can see that

#ifdef YY_USES_REJECT

is replaced by i

f(yy_current_buffer -> yy_is_our_buffer == 0) {

,

and the only the next occurence of

#else

should be replaced to two line i.e

}
else {

and next occurence of

#endif

should be replaced by

}

.

Please let me know how do i achieve this using sed and awk .

Regards,
Vinay

Why would you want to use sed/awk for this? What are you trying to achieve? Did you have a look at the patch command?

@Vinay
Have a look into following link:
https://linuxacademy.com/blog/linux/introduction-using-diff-and-patch/

It uses both diff and patch. Explains you exactly for what you're looking.

Actually i wont be having modified file , i having shown the diff to explain my requirement . Actually we will be having only original file which gets generated while compiling a make file , so in the make file at the time of generation i wanted to introduce all the modification rather the differences which which i have shown . So to meet this requirement , i need to modify the original file only . So for modification i may need sed and awk , if there is any other way also it is fine.

You could try something like:

#!/bin/ksh
end_fmt='\t\t\t}\n'
else_fmt='\t\t\t}\n\t\t\telse {\n'
if_fmt='\t\t\tif(yy_current_buffer -> yy_is_our_buffer == 0) {\n'
if_tag='YY_USES_REJECT'
awk -v end_f="$end_fmt" -v else_f="$else_fmt" -v if_f="$if_fmt" -v t="$if_tag" '
$1 == "#ifdef" && $2 == t {
	iff = 1
	printf if_f
	next
}
iff && $1 == "#else" {
	printf else_f
	next
}
iff && $1 == "#endif" {
	iff = 0
	printf end_f
	next
}
1' makefile

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
If you have a file named makefile that contains:

#ifdef YY_USES_REJECT
				some text
#else
				some more text
#endif
			text after endif
#ifdef YY_OTHER_TAG
				nothing should happen here
#else
				or here
#endif
			or here.  Above #ifdef should not have been changed.
#ifdef YY_USES_REJECT
				some text again
#else
				some more text again
#endif
			text after endif again

the script above will produce the output:

			if(yy_current_buffer -> yy_is_our_buffer == 0) {
				some text
			}
			else {
				some more text
			}
			text after endif
#ifdef YY_OTHER_TAG
				nothing should happen here
#else
				or here
#endif
			or here.  Above #ifdef should not have been changed.
			if(yy_current_buffer -> yy_is_our_buffer == 0) {
				some text again
			}
			else {
				some more text again
			}
			text after endif again
1 Like

Thanks a lot Don Cragun . It is working ..