Skip to content

Simple utility counting the number of rows each pattern of an input list occur in a file.

Notifications You must be signed in to change notification settings

srund/patterncount

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Pattern Count

Introduction

This little script counts on how many rows each input pattern occurs on.

The inspiration was parsing output from ps -ef on a machine with some process families occuring hundreds of times and that had to be monitored.

Installation

This is just a little script that can be run as is.

Usage

./pattern_count.sh [OPTIONS] -p PATTERNS FILENAME
$: ./pattern_count.sh --pattern one,two,three myfile.log

Flags

-p / --pattern : (Required) A list of patterns to match in the file. The list should by default be comma-separated. The patterns are passed on to grep with it's -E flag as is.

-o / --output : (Optional) Output file. Defaults to stdout.

-d / --delimiter : (Optional) Set the pattern list delimiter. Defaults to ,

-c / --count-only : (Optional) Print counts only. Not corresponding patterns.

An Example

Consider a file with the following contents:

one
two
two three
three
three
One
Two
Two Three
Three
Three

The below which contain proper but simple regex expressions, delimited with periods (not best option if using regex), the input file set to testfile.txt, output to outfile.txt and finally the delimiter defined as periods for the list.

$: ./pattern_count.sh --pattern '[Tt]wo.[Oo]ne.[Tt]hree' testfile.txt -o outfile.txt -d '.'

When run we expect the following output:

[Tt]wo : 4
[Oo]ne : 2
[Tt]hree: 6

Note that the rows with both two and three have been counted twice since they matched same both the pattern for two and three.

About

Simple utility counting the number of rows each pattern of an input list occur in a file.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages