awk with range but matches pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with range but matches pattern
# 1  
Old 04-11-2014
awk with range but matches pattern

To match range, the command is:

Code:
awk '/BEGIN/,/END/'

but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this:
Code:
awk '/BEGIN/,/END/ if only in that range there is /pattern/'

Thanks
# 2  
Old 04-11-2014
Code:
awk '/BEGIN/,/END/ { if ($0 ~ /pattern/) print}'

# 3  
Old 04-11-2014
Thanks SriniShoo,

The problem with that command is it will only show the line that contains the pattern, not the range that contains the pattern. How to fix this?
# 4  
Old 04-11-2014
Please provide an input and output so that we can understand your requirement
# 5  
Old 04-11-2014
file content:
Code:
abc
begin
def
ghi
pattern1
end
begin
pattern1
jkl
end
begin
lyt
end
mno
pqr
begin
stu
vwx
yza
end
def
ghi

print lines between "begin" and "end" only if it has pattern1 in that range
The output:

Code:
begin
def
ghi
pattern1
end

begin
pattern1
jkl
end

# 6  
Old 04-11-2014
Try :

Code:
$ awk '/begin/{s=""}{s=s ? s ORS $0:$0}/end/{if(s~/pattern1/)print s RS}' file
begin
def
ghi
pattern1
end

begin
pattern1
jkl
end

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 04-11-2014
Code:
sed -n "/begin/,/end/{H;/begin/h;}; /end/{g;/pattern1/p;}" file

This User Gave Thanks to anbu23 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

2. Shell Programming and Scripting

awk Index to get position matches pattern

Input data as below (filetest.txt): 1|22 JAN Minimum Bal 20.00 | SAT 2|09 FEB Extract bal 168.00BR | REM 3|MIN BAL | LEX Output should be: ( If there is Date & Month in 2nd field of Input file, It should be seperated else blank. If There is Decimal OR Decimal & Currency in last of the 2nd... (7 Replies)
Discussion started by: JSKOBS
7 Replies

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

4. Shell Programming and Scripting

awk to grab data in range then search for pattern

im using the following code to grab data, but after the data in the range im specifying has been grabbed, i want to count how many instances of a particular pattern is found? awk 'BEGIN{count=0} /parmlib.*RSP/,/seqfiles.*SSD/ {print; count++ } /103 error in ata file/ END { print count }'... (3 Replies)
Discussion started by: SkySmart
3 Replies

5. Shell Programming and Scripting

Number of matches and matched pattern(s) in awk

input: !@#$%2QW5QWERTAB$%^&* The string above is not separated (or FS=""). For clarity sake one could re-write the string by including a "|" as FS as follow: !|@|#|$|%|2QW|5QWERT|A|B|$|%|^|&|* Here, I am only interested in patterns (their numbers are variable between records) containing... (16 Replies)
Discussion started by: beca123456
16 Replies

6. UNIX for Dummies Questions & Answers

Extracting range of characters if pattern matches

Im trying compare values between files and if they match I want to extract some characters in between those values for many files. They are in two directories and have the name filename but one ends in .enr. They look like this. cat bat.1.enr name,start,end bat.1,231, 234 and another... (5 Replies)
Discussion started by: verse123
5 Replies

7. Shell Programming and Scripting

[awk] add column between range pattern

Hi all, I have table like this A1 1 2 1 3 2 4 A2 3 1 1 2 1 1 A3 1 1 2 0 3 2 ..... An And i want to add column to my table and the table will become like this : (3 Replies)
Discussion started by: psychop13
3 Replies

8. Shell Programming and Scripting

awk to sum specific field when pattern matches

Trying to sum field #6 when field #2 matches string as follows: Input data: 2010-09-18-20.24.44.206117 UOWEXEC db2bp DB2XYZ hostname 1 2010-09-18-20.24.44.206117 UOWWAIT db2bp DB2XYZ hostname ... (3 Replies)
Discussion started by: ux4me
3 Replies

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

10. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies
Login or Register to Ask a Question