Yesterday’s regex - Was I drunk, or a genius? From Geek & Poke’s cartoons.
I wanted some specific features in a “find” utility, and when I couldn’t figure out how to get them with combinations of
awk, and other Unix commands, I wrote what I wanted in Scala. Those features are (a) showing matching filenames, (b) showing the line that matches my search pattern, and underlining the pattern in the output, (c) showing the line numbers of the matches, and (d) showing an optional number of lines from the file before and after each match.
Linux/Unix FAQ: Can you share some Linux
find command examples?
Sure. The Unix/Linux find command is very powerful. It can search the entire filesystem to find files and directories according to the search criteria you specify. Besides using the
find command to locate files, you can also execute other Linux commands (grep, mv, rm, etc.) on the files and directories you find, which makes find extremely powerful.
Scala FAQ: How can I use regular expression (regex) pattern matching in a
match expression (a Scala match/case expression)?
As I wrote in my Scala sed class post earlier today, Jon Pretty’s Kaleidoscope project lets you use string pattern-matching code in Scala
match expressions. This enables regex pattern-matching code like this:
A few times during the past year I got tired of trying to remember the Unix/Linux
sed syntax while wanting to make edits to many files, so this weekend I wrote a little
sed-like Scala class.
Linux grep commands FAQ: Can you share some Linux/Unix grep command examples?
Sure. The name grep means "general regular expression parser", but you can think of the
grep command as a "search" command for Unix and Linux systems: it's used to search for text strings and more-complicated "regular expressions" within one or more files.
I think it's easiest to learn how to use the
grep command by showing examples, so let's dive right in.
Here’s a quick Scala example that shows how to convert multiple spaces in a string to a single space:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.9, “Extracting Parts of a String that Match Patterns.”
You want to extract one or more parts of a Scala
String that match the regular-expression patterns you specify.
Define the regular-expression (regex) patterns you want to extract, placing parentheses around them so you can extract them as “regular-expression groups.” First, define the desired pattern: