Archive for April, 2019

Cleaning source code with sed

Recently a ticket came across my desk to remove references to Google Plus from a set of email templates our team maintains. With dozens of email templates, I wanted to stretch my command line muscles and see if I could do it all at once.

macOS includes sed, the stream editor, and for this project I’m going to use a very simple set of commands.

Delete lines that match

d is for delete. To use this command, you have a regex, followed by the letter d. It will delete any line that matches.

For example:

sed -i.bak '/googlePlusIcon/d' someFile.txt

Substitute lines

s is for substitute. Think of it like “find and replace” except we are going to find something everywhere. There are 2 parts, a regex to match, and a regex

sed -i.bak 's/googlePlusIcon/twitterIcon/g' someFile.txt

Doing multiple files at once

You can do multiple files at once by combining find with sed. Find syntax is as follows:

find path expression

path is the scope where find will… find things. expression is a set of options to control what we match. For example, -name is to match on the filename.

Another use of expressions is to do some operation. -exec is for executing another command. In this case we want to execute sed. So we type out the sed command from above, except with a few changes. There are curly braces {} which tell find to put the filename of the match in there. There is also the \; which tells find where the -exec expression is ending.

find . -name '*.txt' -exec sed -i.bak 's/googlePlusIcon/twitterIcon/g' {} \;

One quirk about find + sed is that it likes to add newlines to the end of your files. If you are using source control like git, this may cause the diff to be slightly larger than you expected.

Java on Mac Cheat sheet

Install AdoptOpenJDK following instructions for macOS. The instructions don’t say where to install it, I suggest /Library/Java/JavaVirtualMachines. This will get you the built-in mac behavior.

Built-in mac behavior

Older versions of Java created a Java button in System Preferences. It will not show Java 11, but it will show older versions. In my case, I had some old projects using Java 8.

The other built-in is the java_home utility.

List available versions

alias javas_avail='/usr/libexec/java_home -V'

Example output:

Matching Java Virtual Machines (2):
11.0.1, x86_64: "OpenJDK 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
1.8.0_161, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home

Switch to a specific version

java_home will also allow you to switch to a given version. For example

$ export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
$ java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

If you find yourself switching versions a lot, create an alias for it.

Docker cheat sheet

docker run = new container (specify image name)

docker exec = run a command in an existing container

How to turn a docker run command into an interactive shell:

-it --entrypoint=/bin/sh

Note, you have to add that before the name of the image.

Cleanup images

To filter down the images and only clean those matching some-text, use:

docker images | grep some-text | awk '{ print $1 ":" $2}' | xargs docker rmi

Cleanup orphaned images

Sometimes images are orphaned, for example, if you tagged an image as latest, and then built a new image with that same tag. They will show up as <none> in docker images. They can be cleaned up using:

docker rmi $(docker images -f "dangling=true" -q)

List images by size

docker images --format '{{.Size}}\t{{.Repository}}' | sort -hr