print range between two patterns if it contains a pattern within the range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print range between two patterns if it contains a pattern within the range
# 1  
Old 06-18-2009
print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated.

e.g.

Pattern1
Bombay
Calcutta
Delhi
Pattern2

Pattern1
Patna
Madras
Gwalior
Delhi
Pattern2

Pattern1
Guwahati
Calcutta
Indore
Pattern2


I want to select the block which contains "Calcutta". So the desired output is

Pattern1
Bombay
Calcutta
Delhi
Pattern2


Please help !!!!!!!!!! Thanks again
# 2  
Old 06-18-2009
Given myFile:
Code:
Pattern1
Bombay
Calcutta
Delhi
Pattern2

Pattern1
Patna
Madras
Gwalior
Delhi
Pattern2

Pattern1
Guwahati
Calcutta
Indore
Pattern2

'Calcutta' appears in 2 different blocks.
Code:
nawk '/Calcutta/' RS= FS= ORS='\n\n' myFile

# 3  
Old 06-18-2009
Quote:
Originally Posted by vgersh99
Given myFile:
Code:
Pattern1
Bombay
Calcutta
Delhi
Pattern2
 
Pattern1
Patna
Madras
Gwalior
Delhi
Pattern2
 
Pattern1
Guwahati
Calcutta
Indore
Pattern2

'Calcutta' appears in 2 different blocks.
Code:
nawk '/Calcutta/' RS= FS= ORS='\n\n' myFile

Thanks so much...............
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match patterns between two files and extract certain range of strings

Hi, I need help to match patterns from between two different files and extract region of strings. inputfile1.fa >l-WR24-1:1 GCCGGCGTCGCGGTTGCTCGCGCTCTGGGCGCTGGCGGCTGTGGCTCTACCCGGCTCCGG GGCGGAGGGCGACGGCGGGTGGTGAGCGGCCCGGGAGGGGCCGGGCGGTGGGGTCACGTG... (4 Replies)
Discussion started by: bunny_merah19
4 Replies

2. UNIX for Beginners Questions & Answers

TCL script to print range of lines between patterns

Hi I am having a code as stated below module abcd( a , b , c ,da , fa, na , ta , ma , ra , ta, la , pa ); input a , b, da ,fa , na , ta , ma; output c , ra ,ta , la ,pa ; wire a , b , da , fa ,na , ta , ma; endmodule I need to match the string... (1 Reply)
Discussion started by: kshitij
1 Replies

3. Shell Programming and Scripting

sed Range Pattern and 2 lines before Start Pattern

Hi all, I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve. Consider the following input: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

4. Shell Programming and Scripting

sed command to print first instance of pattern in range

The following text is in testFile.txt: one 5 two 10 three 15 four 20 five 25 six 10 seven 35 eight 10 nine 45 ten 50 I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency,... (9 Replies)
Discussion started by: uschaafm
9 Replies

5. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

6. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

7. Shell Programming and Scripting

Print pattern range to a new file

Hi Everyone! I really appreciate all of your help, I'm learning so much, can't wait until I get good enough to start answering questions! I have a problem ... from one large file, I'd like to create multiple new files for each pattern block beginning with /^ISA/ ending with /^IEA/ ... (2 Replies)
Discussion started by: verge
2 Replies

8. Shell Programming and Scripting

range patterns in awk

Hi All, I am new to awk command and I had a question in using it. I want to filter a code file and print specific functions (that contain menu word in the function name). for example, if the file has: function menu1() { } function f2() { } function menu3() { }so I want... (5 Replies)
Discussion started by: ghoda2_10
5 Replies

9. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

10. Shell Programming and Scripting

sed pattern range

Hi guys, trying to replace a '#' with a ' ' (space) but only between the brackets '(' and ')' N="text1#text2#text3(var1#var2#var3)" N=`echo $N |sed '/(/,/) s/#. //'` echo $N Looking for an output of "text1#text2#text3(var1 var2 var3)" Any ideas? (15 Replies)
Discussion started by: mikepegg
15 Replies
Login or Register to Ask a Question