Need help with awk tool - finding text between two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with awk tool - finding text between two patterns
# 1  
Old 12-16-2009
Need help with awk tool - finding text between two patterns

This is regarding using awk tool to find lines matching between 2 patterns.
Code:
cat file | awk '/pat1/,/pat2/'


But it's not working as expected in the following case.

[1]

If pat1 also comes after pat2 then it's matching whole file after pat1.

e.g.

Code:
# > cat -n file
     1  First line
     2  contains pat1
     3  intermediate lines
     4  pat2
     5  some line
     6  again here pat1
     7  end of file

# > cat file | awk '/pat1/,/pat2/'
contains pat1
intermediate lines
pat2
again here pat1
end of file


Here I want only lines between first pat1 and first pat2 and want to ignore all other lines.
How to do that? Can we use sed so that this problem does not arise?

One solution that came in my mind is that change only the first pat1 by say PAT1 and then use awk '/PAT1/,/pat2/', but I am sure there are better solutions.

Last edited by Franklin52; 12-16-2009 at 04:32 AM.. Reason: Please use code tags!
# 2  
Old 12-16-2009
Something like this?

Code:
awk '/pat1/{p=1}/pat2/{print;exit}p' file

# 3  
Old 12-16-2009
Code:
gawk -vRS="pat2" '{gsub(/.*pat1/,"");print;exit}' file

Code:
gawk '/pat1/,/pat2/ {if($0~/pat2/){print;exit}else{print}}' file


Last edited by ichigo; 12-16-2009 at 05:41 AM..
# 4  
Old 12-16-2009
Quote:
Originally Posted by ichigo
Code:
gawk -vRS="pat2" '{gsub(/.*pat1/,"");print;exit}' file

Have you tried your command?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding matching patterns in two files

Hi, I have requirement to find the matching patterns of two files in Unix. One file is the log file and the other is the error list file. If any pattern in the log file matches the list of errors in the error list file, then I would need to find the counts of the match. For example, ... (5 Replies)
Discussion started by: Bobby_2000
5 Replies

2. Shell Programming and Scripting

Finding files which contains anyone from the given patterns

Hi All, I need help as i am not able to create shell script for a scenario. i have 3000 numbers and want to search all the files which contain anyone of the above pattern. the files are in folder structure. Thanks and Regards Rishi Dhawan (14 Replies)
Discussion started by: Rishi26
14 Replies

3. Shell Programming and Scripting

Replacing text between two patterns

I would like to replace ], with ]]], between /* SECTION2-BEGIN */ and /* SECTION2-END */ in my file. My file contains the following information: /* SECTION1-BEGIN */ , /* SECTION1-END */ /* SECTION2-BEGIN */ , /* SECTION2-END */ /*... (5 Replies)
Discussion started by: azdps
5 Replies

4. Shell Programming and Scripting

Finding several patterns and outputting 4 lines after

I an trying to parse a file looking for pattern1, or pattern2, or pattern3 and when found print that line and the next 4 lines after it. I am using korn shell script on AIX and grep -A isn't available. (1 Reply)
Discussion started by: daveisme
1 Replies

5. Shell Programming and Scripting

Finding patterns in a file

Hi, I have a file with 3 columns and I want to find when the average number of rows on column 3 is a certain value. The output will be put into another file indicating the range. Here is what I mean (file is tab separated): hhm1 2 0 hhm1 4 0.5 hhm1 6 0.3 hhm1 8 -1.4... (2 Replies)
Discussion started by: kylle345
2 Replies

6. Shell Programming and Scripting

noob question - is awk the tool to clean dirty text files?

Hi, nevermind. I think I've found the answer. It appears I was looking for index, match, sub, and gsub. I want to write a shell script that will clean the html out of a bunch of files and format the data for import into excel. Awk seems like a powerful tool, but it seems oriented to... (1 Reply)
Discussion started by: yogert909
1 Replies

7. Shell Programming and Scripting

finding and removing patterns in a large list of urls

I have a list of urls for example: Google Google Base Yahoo! Yahoo! Yahoo! Video - It's On Google The problem is that Google and Google are duplicates as are Yahoo! and Yahoo!. I'm needing to find these conical www duplicates and append the text "DUP#" in from of both Google and... (3 Replies)
Discussion started by: totus
3 Replies

8. UNIX for Dummies Questions & Answers

Copying Text between two unique text patterns

Dear Colleagues: I have .rtf files of a collection of newspaper articles. Each newspaper article starts with a variation of the phrase "Document * of 20" and is separated from the next article with the character string "===================" I would like to be able to take the text composing... (3 Replies)
Discussion started by: spindoctor
3 Replies

9. Programming

Tool for finding memory leaks

hi, i am a c++ programmer working on linux(redhat linux8.0) environment, i need to find out the memory leaks, so far i didn't used any tools, so what are the tools are available, and whic one is good to use. plz provide with a small example. (1 Reply)
Discussion started by: sarwan
1 Replies

10. Shell Programming and Scripting

Finding patterns through out all subdir

Hi all experts, here is a problem which i would appreciate ur expertise. I need to do this: Eg. Find a number: 1234567 which i dunno which file and which folder I do know which main folder it is in but it is hidden deep within a lot of subdir. Is it possible to find the file? + output... (4 Replies)
Discussion started by: unnerdy
4 Replies
Login or Register to Ask a Question