Matt Gifford aka coldfumonkeh | Consultant Developer
View Github Profile


grep with CFML and Java Streams

Oct 23, 2019

I was listening to “Modernize or Die® - CFML News for October 15th, 2019” this morning, and heard Andrew Davis ask “Is there anything in ColdFusion somewhere to grep, aside from just doing a cfexecute on the grep?”. The link to the exact point in the awesome podcast video stream is here.

This got me thinking.. yeah, not normally a good sign.

I know that you can read files in Java 8 using the streams, so in theory you could perform at least a regex filter on file contents as a pseudo-grep.

So, I got writing during my lunch break and came up with grep.cfc.

Before anyone judges me on anything, this was literally a 20 minute hack to play around and see what I could come up with.

As an aside, I only started listening to the podcast yesterday, so I have a lot to catch up on, but I already love it and I think the Ortus team are doing an amazing job with it! Needless to say, I’m now a subscriber.

Anyway…

Grepping

At the moment it only handles a grep filter on a single file, although I plan to add directory support to it as iterating over directories using streams will be relatively straight-forward and fast.

I’ve broken the component down into simple chained method calls.

So, example one and I’m searching for every instance of public within the given file. In these examples, I’m actually grepping against the grep.cfc file itself; I’m not sure if that is “grep-ception”.

Firstly we provide a pattern to match (pattern()), the we set a relative path to the file to search (file()).

Finally, to complete the process we call the grep() method which creates the initial Java stream on the file, pulling out all of the lines contained within. The component then creates a Java Predicate using the pattern string value sent via the pattern() method call. This predicate is then used to filter any lines that match the given pattern, before terminating the stream by returning an array of results.

The resulting dump from the above example is here:

The response from grep 1

Running a second example, I’m trying to grep on my domain name:

This results in just the one item coming back in the array:

The response from grep 2

It doesn’t return line numbers when perhaps it should. Agsin, before anyone judges me on anything, this was literally a 20 minute hack to play around and see what I could come up with.

You all know I love cbStreams. It could do most if not all of this as well, but I wanted to try and come up with something quickly without dependencies for an extra challenge.

It is available on Github, but the bare bones are below in the gist.

That’s about it. Just another quick experiment to see if something was possible.


Latest Blog Posts

Jul 16, 2020
Github Actions with CommandBox and TestBox
Read More
Jul 9, 2020
Azure pipelines with CommandBox and TestBox
Read More
Dec 23, 2019
CFML content moderation detection component library
Read More