Sponsored Content
Top Forums UNIX for Advanced & Expert Users Unix: list out Pattern occurrence (count) Post 302546282 by itkamaraj on Wednesday 10th of August 2011 11:26:11 PM
Old 08-11-2011
Code:
 
$ nawk '{for(i=1;i<=NF;i++)if($i == "apple") ++count;} END{print count}' test
8
$ cat test
apple apple
ball ball
apple
ball ball ball
apple apple apple
cat cat
cat cat cat
apple
apple

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

count a occurrence

I am looking to get a output of "2 apple found" from the awk command below. black:34104 tomonorisoejima$ cat tomo apple apple black:34104 tomonorisoejima$ awk '/apple/ {count++}END{print count " apple found"}' tomo 1 apple found black:34104 tomonorisoejima$ (5 Replies)
Discussion started by: soemac
5 Replies

2. Shell Programming and Scripting

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: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

3. Shell Programming and Scripting

Count number of occurrence at each line

Hi I have the following file ENST001 ENST002 4 4 4 88 9 9 ENST004 3 3 3 99 8 8 ENST009 ENST010 ENST006 8 8 8 77 8 8 Basically I want to count how many times ENST* is repeated in each line so the expected results is 2 1 3 Any suggestion please ? (4 Replies)
Discussion started by: fuad_
4 Replies

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

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

6. Shell Programming and Scripting

How to count the occurrence of a number?

Hi, I have a file which contained a set of numbers like Col1 col2 col3 col4 1 sa 13 0 2 sb 14 0 3 sc 15 9 4 sd 16 -9 5 sd 20 -2 6 sd 20 4 Here in last column I need to count the zeros, positive values and negative values, please help me to do that. (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

7. Shell Programming and Scripting

Count occurrence of string in a column using awk

Hi, I want to count the occurrences of strings in a column and display as in example below: Input: get1 345 789 098 get2 567 982 090 fet4 777 610 632 get1 800 544 230 get1 600 788 451 get2 892 321 243 get1 673 111 235 fet3 789 220 278 fet4 768 222 341 output: 4 get1 345 789... (7 Replies)
Discussion started by: aydj
7 Replies

8. UNIX for Dummies Questions & Answers

Count occurrence of column3 in column2

count occurrence of column3 in column2 with awk Input: AA BB BB AA BB CC AA BB CC AA CC BB CC CC BB BB Output: AA BB 3 (1 Reply)
Discussion started by: aydj
1 Replies

9. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

10. Shell Programming and Scripting

Count of occurrence in particular column of the file.

Hi All, let's say an input looks like: C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 ---------------------------------- 1|0123452|C501|Z|Z|Z|E|E|E|E|E|E|E 1|0156123|C501|X|X|X|E|E|E|E|E|E|E 1|0178903|C501|Z|Z|Z|E|E|E|E|E|E|E 1|0127896|C501|Z|Z|Z|E|E|E|E|E|E|E 1|0981678|C501|X|X|X|E|E|E|E|E|E|E ... (6 Replies)
Discussion started by: suresh_target
6 Replies
xpc_events(3)						   BSD Library Functions Manual 					     xpc_events(3)

NAME
xpc_events -- launch-on-demand for high-level events SYNOPSIS
#include <xpc/xpc.h> void xpc_set_event_stream_handler(const char *name, dispatch_queue_t targetq, xpc_handler_t handler); DESCRIPTION
XPC provides a mechanism by which launchd jobs may launch on-demand for certain higher-level events, such as IOKit events or BSD Notifica- tions. These events are delivered to the job through a handler that is set early in its execution. The period between when the event is delivered to the job and when a handler is set is race-free, and any pending events will be queued up for consumption by the job. An event is consumed when it is delivered to the handler. EVENT STREAMS
Providers of events are known as streams. Two example event streams are the IOKit stream and the BSD Notifications stream. Streams are denoted by a reverse-DNS naming scheme. For the aforementioned examples, the stream names are "com.apple.iokit.matching" and "com.apple.noti- fyd.matching", respectively. These are currently the only two supported event streams. EVENT NAMES
A launchd job may be interested in multiple events from different event streams. Each of these events has a name provided by the job in the launchd.plist(5). The occurrence of any of these events will launch the job on-demand if it is not already running. PLIST SCHEMA
Events are specified through the launchd.plist(5) with the LaunchEvents key. The value for this key is a dictionary. Each value of this dic- tionary is itself a dictionary corresponding to an event stream. The values of this inner dictionary are events that may cause the job to be launched on-demand. <key>LaunchEvents</key> <dict> <key>com.apple.iokit.matching</key> <dict> <key>com.apple.device-attach</key> <dict> <key>idProduct</key> <integer>2794</integer> <key>idVendor</key> <integer>725</integer> <key>IOProviderClass</key> <string>IOUSBDevice</string> <key>IOMatchLaunchStream</key> <true/> </dict> </dict> <key>com.apple.notifyd.matching</key> <dict> <key>com.apple.interesting-notification</key> <dict> <key>Notification</key> <string>com.apple.interesting-notification</string> </dict> </dict> </dict> The above specifies that the job will be launched when a node matching the given matching dictionary appears in the IORegistry or when a notification named "com.apple.interesting-notification" is posted using notify_post(3). NOTE: The IOMatchLaunchStream key is required to be present and be a Boolean set to true for use with XPC Events. It will be filtered out of the rest of the dictionary when given to IOKit to match. The reasons for this are historical and not applicable to other event streams. Each event stream has a different plist schema. EVENT CONSUMPTION
Events are consumed with the xpc_set_event_stream_handler() API. The stream argument specifies from which event stream the given handler will receive events. The targetq parameter specifies on which queue the handler will be synchronized. The handler will only ever receive dictio- naries. Each dictionary is guaranteed to have the XPC_EVENT_KEY_NAME key set. The value for this key is the string that was given as the name for the event in the launchd.plist(5). So if the IOKit event in the above example was received, the value of this key would be "com.apple.device-attach". In addition to the standard payload, events from the IOKit stream also have the "IOMatchLaunchServiceID" key set to a uint64_t which speci- fies the unique IORegistry ID of the node which matched the given dictionary as obtained by IORegistryEntryGetRegistryEntryID(). This value may be given to IORegistryEntryIDMatching() to obtain the registry entry which caused the event to fire. BSD Notfication events have no additional payload. xpc_set_event_stream_handler("com.apple.iokit.matching", q, ^(xpc_object_t event) { const char *name = xpc_dictionary_get_string(event, XPC_EVENT_KEY_NAME); uint64_t id = xpc_dictionary_get_uint64(event, "IOMatchLaunchServiceID"); CFMutableDictionaryRef matching = IORegistryEntryIDMatching(id); // Pass to IOServiceGetMatchingServices() or IOServiceAddNotification(). }); IMPORTANT: xpc_set_event_stream_handler() is NOT shareable. Two different subsystems in a process cannot safely both register for events from the same event stream. Therefore, libraries and frameworks should NEVER call this API. SEE ALSO
xpc_object(3), xpc_dictionary_create(3), xpc_array_create(3), notify(3) Darwin 1 July, 2011 Darwin
All times are GMT -4. The time now is 10:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy