Visit Our UNIX and Linux User Community


Count the number of occurrences of a pattern between each occurrence of a different pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count the number of occurrences of a pattern between each occurrence of a different pattern
# 1  
Old 11-09-2009
Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'.

Here's a portion of the text I'm trying to parse:

Code:
lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1  p-reserver IID 0xdb
  registrations:
   key 4156 4353 0000 0000
   initiator (217) fcp portName=0x10000000c9862398
   target (6) fcp portName=0x2007000123000591 nodeName=0x2000000123000591

   key 4156 4353 0000 0000
   initiator (217) fcp portName=0x10000000c9862398
   target (18936) fcp portName=0x20070001230004e1 nodeName=0x20000001230004e1

   key 4156 4353 0000 0000
   initiator (217) fcp portName=0x10000000c9862398
   target (0) fcp portName=0x20070001230004f5 nodeName=0x20000001230004f5

   key 4156 4353 0000 0000
   initiator (217) fcp portName=0x10000000c9862398
   target (34960) fcp portName=0x20070001230004c1 nodeName=0x20000001230004c1

   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (54648) fcp portName=0x20080001230004e1 nodeName=0x20000001230004e1

   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (6) fcp portName=0x20080001230004f5 nodeName=0x20000001230004f5

   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (7) fcp portName=0x2008000123000591 nodeName=0x2000000123000591

   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (7) fcp portName=0x20080001230004c1 nodeName=0x20000001230004c1


lu S5678L_1FF_m1_vg.2, part-att 1, vdp-att 1  p-reserver IID 0xdb
  registrations:
   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (12336) fcp portName=0x20080001230004f5 nodeName=0x20000001230004f5

   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (7) fcp portName=0x20080001230004c1 nodeName=0x20000001230004c1

   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (7) fcp portName=0x20080001230004e1 nodeName=0x20000001230004e1

   key 4156 4353 0000 0000
   initiator (219) fcp portName=0x10000000c9862399
   target (7) fcp portName=0x2008000123000591 nodeName=0x2000000123000591

Here's the output I'm hoping to achieve:

Code:
$ someawkcommand myexample.txt
lu S1234L_149_m1_vg.6: 8
lu S5678L_1FF_m1_vg.2: 4

With my rudimentary awk skills I thought I might build on answers to similar questions in the forum by creating separate temporary files for each occurrence of 'lu' and counting the occurrences of 'key' with grep. I think I'm fighting Darwin's implementation of awk though, e.g.:

Code:
awk '/lu/{close("file"f);f++}{print $0 > "file"f}' myexample.txt
awk: syntax error at source line 1
 context is
	/lu/{close("file"f);f++}{print $0 > >>>  "file"f <<< }
awk: illegal statement at source line 1

I'm also not sure if this something better suited for building with sed, e.g. something including:

Code:
sed '/lu/,/lu/ p' myexample.txt

Thanks for any help or examples you can point me to..
# 2  
Old 11-09-2009
Code:
$ gawk -vRS="lu" 'NF{m=gsub("key","");print "lu",$1,m} ' file
lu S1234L_149_m1_vg.6, 8
lu S5678L_1FF_m1_vg.2, 4

# 3  
Old 11-09-2009
perfect solution.

Code:
 The gsub function returns the number of substitutions made.

# 4  
Old 11-10-2009
Code:
my $cnt=0;
open FH,"<yourfile.txt";
while(<FH>){
	if(/(lu\s+[^ ,]*)/){
		if($cnt!=0){
			print " ",$cnt,"\n";
			$cnt=0;
		}
		print $1;		 
	}
	if(/^\s+key/){
		$cnt+=1;
	}
}
print " ",$cnt,"\n";


Previous Thread | Next Thread
Test Your Knowledge in Computers #570
Difficulty: Medium
The C programming language allows you to pass a parameter to a function by providing its memory address instead of the value stored in it.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to Serach pattern and give number of occurrences

Hi, I want a script which search for a pattern "good" in a huge file and provide me number of occurences of such pattern in a file. lets say i have a file test.txt contents as below good is good but good is sometime bad and sometime good you are very good and good is always good ... (7 Replies)
Discussion started by: sv0081493
7 Replies

2. Shell Programming and Scripting

Match from one pattern to second occurrence of second pattern

Given an XML file that contains (NOT "consists of"): </dict> <key>system.</key> <dict> <key>rule</key> <string>default</string> </dict> <key>system.burn</key> ... (9 Replies)
Discussion started by: jnojr
9 Replies

3. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

4. UNIX for Advanced & Expert Users

Unix: list out Pattern occurrence (count)

Need to search a pattern occurrence (count) in a specified file. Below is the details $ cat fruits apple apple ball ball apple ball ball ball apple apple apple cat cat cat cat cat apple apple Note: If I'll use the grep command with -c option then it'll count the 1st occurrence in... (6 Replies)
Discussion started by: tp2115
6 Replies

5. Shell Programming and Scripting

How to count the number of files starting with a pattern in a Directory

Hi! In our current directory there are around 35000 files. Out of these a few thousands(around 20000) start with, "testfiles9842323879838". I want to count the number of files that have filenames starting with the above pattern. Please help me with the command i could use. Thank... (7 Replies)
Discussion started by: atechcorp
7 Replies

6. Shell Programming and Scripting

counting number of pattern occurrences

Hi All, Is it possible to count number of occurrences of a pattern in a single record using awk?? for example: a line like this: abrsjdfhafa I want to count the number of a character occurrences. but still use the default RS, I don't want to set RS to single character. (1 Reply)
Discussion started by: ghoda2_10
1 Replies

7. UNIX for Dummies Questions & Answers

line number of the i-th occurrence of a pattern

Hi all, is there a simple way to obtain the line number of the i-th occurrence of a pattern? I have OCCURRENCE=`grep -io "${STRING_NAME}" ${1}-${8}${EXT}.out_bis| wc -l` which tells me how many occurency I have. I would like to go through them and determine the line number and assign... (6 Replies)
Discussion started by: f_o_555
6 Replies

8. UNIX for Dummies Questions & Answers

Search and Count Occurrences of Pattern in a File

I need to search and count the occurrences of a pattern in a file. The catch here is it's a pattern and not a word ( not necessarily delimited by spaces). For eg. if ABCD is the pattern I need to search and count, it can come in all flavors like (ABCD, ABCD), XYZ.ABCD=100, XYZ.ABCD>=500,... (6 Replies)
Discussion started by: tektips
6 Replies

9. Shell Programming and Scripting

How to count the number of occurences of this pattern?

Hi all, I have a pattern like this in a file: 123 4 56 789 234 5 67 789 121 3 56 789 222 4 65 789 321 6 90 100 478 8 40 789 243 7 80 789 How can I count the number of occurences of '789' (4th column) in this set...? Thanks for all your help! K (7 Replies)
Discussion started by: kripssmart
7 Replies

10. Shell Programming and Scripting

nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes. From server to server, the LUNs sizes differ. What I want to do is count the occurances as they occur and change. These are the LUN sizes: 49.95 49.95 49.95 49.95 49.95 49.95 49.95 49.95... (2 Replies)
Discussion started by: cyber111
2 Replies

Featured Tech Videos