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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3
# 1  
Old 06-11-2014
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.

Code:
[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
Code:
#ifdef YY_USES_REJECT

is replaced by i
Code:
f(yy_current_buffer -> yy_is_our_buffer == 0) {

,

and the only the next occurence of
Code:
#else

should be replaced to two line i.e
Code:
}
else {


and next occurence of
Code:
#endif

should be replaced by
Code:
}

.

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

Regards,
Vinay

Last edited by Scrutinizer; 06-11-2014 at 05:39 AM.. Reason: code tags
# 2  
Old 06-11-2014
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?
# 3  
Old 06-11-2014
@Vinay
Have a look into following link:
https://linuxacademy.com/blog/linux/...iff-and-patch/

It uses both diff and patch. Explains you exactly for what you're looking.
# 4  
Old 06-11-2014
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.
# 5  
Old 06-11-2014
You could try something like:
Code:
#!/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:
Code:
#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:
Code:
			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

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 06-12-2014
Thanks a lot Don Cragun . It is working ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

Sed/awk : to grep only required pattern disk

Hi Experts, Need help with the following: Desired output: Only want to get the output marked in green. The file: --- Physical volumes --- PV Name /dev/disk/disk4704 PV Status available Total PE 6399 Free PE ... (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

6. UNIX for Dummies Questions & Answers

One liner pattern search with awk/sed/grep

I have an array containing bunch of characters. I have to check this array for specific character and if "Not Found than" use a goto statement to go to USAGE set options = (A B C D E F) @ i = 0 while ($i <= ${#options}) if ($options != "F" || $options != "D") then goto USAGE endif @... (1 Reply)
Discussion started by: dixits
1 Replies

7. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

8. Shell Programming and Scripting

How to awk/sed/grep lines which contains a pattern at a given position

Dear friends I am new to linux and was trying to split some files userwise in our linux server. I have a data file of 156 continuous columns named ecscr final. I want the script to redirect all the lines containing a pattern of 7 digits to separate files. I was using grep to do that,... (2 Replies)
Discussion started by: anoopvraj
2 Replies

9. Shell Programming and Scripting

Extracting pattern only with AWK | SED | GREP

We have the following statement working in CGYWIN, but when we move the program to Solaris 10 it fails. x=`echo "ABC196925XYZ" | grep -o --only-matching "\{6\}"` How can we use AWK or SED to extract only the number from the string? The following outputs the entire string. We only want... (5 Replies)
Discussion started by: James Clark
5 Replies

10. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies
Login or Register to Ask a Question