Help needed in sed range pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in sed range pattern
# 1  
Old 02-26-2008
Help needed in sed range pattern

Hi all,

I am using sed for extracting the lines that occurs between the 2 patterns using the following command:

sed -n '/pattern1/,/pattern2/' filename

The above command has no problem and works fine. But I was wondering if there is a way to quit sed when it has extracted the range at least once ? this is required because the file will have only one occurrence of the above pattern range and also the input file on which sed operates is big.

thanks in advance.
sank.
# 2  
Old 02-26-2008
Quote:
Originally Posted by sank
Hi all,

I am using sed for extracting the lines that occurs between the 2 patterns using the following command:

sed -n '/pattern1/,/pattern2/' filename

The above command has no problem and works fine. But I was wondering if there is a way to quit sed when it has extracted the range at least once ? this is required because the file will have only one occurrence of the above pattern range and also the input file on which sed operates is big.

Code:
sed -n -e '/pattern1/,/pattern2/' -e '/pattern2/q' filename

# 3  
Old 02-27-2008
Hi,

thanks for the reply. It works fine when pattern1 and pattern2 are totally different. In some cases, pattern2 will be a part of pattern1 and in this case, it will just print the pattern1 and exits. how can we avoid this ?

Also, pattern2 can occur ( multiple times ) before pattern1 itself since it can pair up with other patterns to form a different range. so, how can we modify the command so that "-e '/pattern2/q' " part is effective only if the search is in the range ?

thanks again,
sank.
# 4  
Old 02-27-2008
Try:
Code:
awk '/pattern2/&&f{exit}f;/pattern1/{f++}' file


Last edited by Klashxx; 02-27-2008 at 06:06 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

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

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

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

sed replace after pattern help needed

Hello, I have a file with multiple lines like this: /film/4295/"_class="titre_article">50/50I would like to change all occurence of / after > with _ to have this: /film/4295/"_class="titre_article">50_50Thank you edit: This could also be change all / starting with the 4th occurrence... (2 Replies)
Discussion started by: patx
2 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

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

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

10. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: joyan321
2 Replies
Login or Register to Ask a Question