1. search 2nd pattern after a pattern and summarize stats


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 1. search 2nd pattern after a pattern and summarize stats
# 1  
Old 01-12-2011
1. search 2nd pattern after a pattern and summarize stats

I have two questions. I am sure one of the Guru will be able to help either one or both.

1. Find 2nd occurance of pattern= "Bind variable after pattern="ABN USER Admin"
......
ABN USER Admin <--- I know this string
.....
Bind variable
...
..
Bind variable <-- Want to print this string(2nd occurnace)

2. I have lot of statement with timing information and want to summarize in some brackets. e.g
0 to 10 seconds 10
10 to 20 seconds 12
20 to 50 second 3

e,g
actual data
SQL Statement execution time 0.001 Seconds
SQL Statement execution time 10.12 Seconds
SQL statement execution time 22 Seconds
SQL statement execution time 5.08 Seconds
SQL statement execution time 12 Seconds
SQL statement execution time 19.08 Seconds
SQL statement execution time 20.01 Seconds

output expected
0 to 10 seconds --> 2
10 to 20 seconds -->3
20 to 30 seconds --> 2

Thanks in advance for your time.
# 2  
Old 01-12-2011
For the 1st question, I am not quite clear about what you want to achieve, for the 2nd question:
Code:
awk '{ 
 if($5<=10) {++b1} 
 else if($5>10&&$5<=20) {++b2} 
 else if($5>20&&$5<=30) {++b3} 
 else {++b4} } 
END { 
 printf "0 to 10 seconds --> %d\n", b1;
 printf "11 to 20 seconds --> %d\n", b2;
 printf "21 to 30 seconds --> %d\n", b3;
 printf "more than 30 seconds --> %d\n", b4;
}' infile

# 3  
Old 01-12-2011
issue 2 resolved. issue 1 need help

Yes, your answer exactly does what is needed. Thank you very much.

For question 1 I need to file 2nd occurance of particular word starting from a particular word in file.
e.g find the second occurance of pattern2 after pattern1

1.pattern2
2.pattern2
3.pattern1
4.pattern2
5.pattern2

output 5.pattern2 (this is 2nd occurance from pattern1)
# 4  
Old 01-13-2011
Code:
awk '/pattern1/{cnt=0;p=1;next} /pattern2/{cnt++;if(p && cnt==2){print $0;p=0}}' inputFile

# 5  
Old 01-13-2011
Code:
awk '/pattern1/{c=2} /pattern2/&& c--==1' file

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

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

3. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

4. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

7. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

8. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question