Last active
March 9, 2016 20:24
-
-
Save spareslant/04b2329f0de4ca324980 to your computer and use it in GitHub Desktop.
Grep like utility that prints delimiter after a successful group match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// This program will act like egrep -f patternfile textfile. But it will also print a delimeter ======= after each section of successfull match | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"regexp" | |
"strings" | |
) | |
type GroupMatch struct { | |
currentmatchedline int | |
patterns []string | |
} | |
func NewGroupMatch(str []string) *GroupMatch { | |
g := &GroupMatch{0, str} | |
return g | |
} | |
func (g *GroupMatch) domatching(line string) { | |
line = strings.TrimRight(line, "\n") | |
for i, pattern_line := range g.patterns { | |
matched, err := regexp.MatchString(regexp.QuoteMeta(pattern_line), line) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if matched { | |
fmt.Printf("%s\n", line) | |
g.currentmatchedline = i | |
} | |
} | |
} | |
func (g *GroupMatch) detect_end_of_group() { | |
current_matched_line := g.currentmatchedline + 1 | |
if len(g.patterns) == current_matched_line { | |
fmt.Printf("===========\n") | |
} | |
} | |
func main() { | |
if len(os.Args) != 3 { | |
fmt.Println("Usage is " + os.Args[0] + " <patternfile> <subjectfile") | |
os.Exit(1) | |
} | |
patternfile := os.Args[1] | |
subjectfile := os.Args[2] | |
var pattern_lines []string | |
pattern_file, err := os.Open(patternfile) | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
defer pattern_file.Close() | |
pr := bufio.NewReader(pattern_file) | |
for line, err := pr.ReadString('\n'); err == nil; { | |
if (err != nil) && (err != io.EOF) { | |
log.Fatal(err) | |
break | |
} | |
line = strings.TrimRight(line, "\n") | |
pattern_lines = append(pattern_lines, line) | |
line, err = pr.ReadString('\n') | |
} | |
file, err := os.Open(subjectfile) | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
defer file.Close() | |
reader := bufio.NewReader(file) | |
for line, err := reader.ReadString('\n'); err == nil; { | |
if (err != nil) && (err != io.EOF) { | |
log.Fatal(err) | |
break | |
} | |
g := NewGroupMatch(pattern_lines) | |
g.domatching(line) | |
g.detect_end_of_group() | |
line, err = reader.ReadString('\n') | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# This program will act like egrep -f patternfile textfile. But it will also print a delimeter ======= after each section of successfull match | |
import os, sys, re | |
class Groupmatch: | |
def __init__(self, pattern_arr): | |
self.currentmatchedline = 0 | |
self.pattern_arr = pattern_arr | |
def domatchingline(self, line): | |
line = line.rstrip('\n') | |
for index, pattern_line in enumerate(self.pattern_arr): | |
re_object=re.compile(re.escape(pattern_line)) | |
if re_object.search(line): | |
print line | |
self.currentmatchedline = index + 1 | |
def detect_end_of_group(self): | |
if self.currentmatchedline == len(self.pattern_arr): | |
print "================" | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
print "usage is {} <patternfile> <subjectfile>".format(sys.argv[0]) | |
exit(2) | |
patternfile=sys.argv[1] | |
subjectfile=sys.argv[2] | |
if not os.path.exists(patternfile): | |
print "{} file does not exist".format(patternfile) | |
exit(2) | |
if not os.path.exists(subjectfile): | |
print "{} file does not exist".format(patternfile) | |
exit(2) | |
f = open(patternfile) | |
pattern_arr = f.read().splitlines() | |
f.close() | |
f = open(subjectfile) | |
for line in f: | |
gmatch=Groupmatch(pattern_arr) | |
gmatch.domatchingline(line) | |
gmatch.detect_end_of_group() | |
gmatch=None | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# | |
# This program will act like egrep -f patternfile textfile. But it will also print a delimeter ======= after each section of successfull match | |
if ARGV.length != 2 | |
puts "usage is $0 <patternfile> <subjectfile>" | |
exit 1 | |
end | |
patternfile = ARGV[0] | |
subjectfile = ARGV[1] | |
if not File.exist?(subjectfile) | |
puts "#{subjectfile} does not exists" | |
exit 1 | |
end | |
if not File.exist?(patternfile) | |
puts "#{patternfile} does not exists" | |
exit 1 | |
end | |
pattern_arr = File.readlines(patternfile) | |
class Groupmatch | |
def initialize(pattern_arr) | |
@currentmatchedline = 0 | |
@pattern_arr = pattern_arr | |
end | |
def domatching(line) | |
@pattern_arr.each_with_index do |pattern, index| | |
pattern.strip! | |
if line =~ %r!#{Regexp.quote(pattern)}! | |
puts line | |
@currentmatchedline = index + 1 | |
end | |
end | |
end | |
def detect_end_of_group() | |
if @currentmatchedline == @pattern_arr.length | |
puts '===========' | |
end | |
end | |
end | |
File.open(subjectfile).each do |line| | |
#puts "PROCESSING - #{line}" | |
gmatch = Groupmatch.new(pattern_arr) | |
gmatch.domatching(line) | |
gmatch.detect_end_of_group() | |
gmatch = nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment