search

How to replace newline character with sed on Mac OS X (macOS)

I don’t have much time to explain this today, but ... if you want to see how to use the sed command on a Mac OS X (macOS) system to search for newline characters in the input pattern and replace them with something else in the replacement pattern, this example might point you in the right direction.

How to search the history of my Facebook posts

Facebook FAQ: How do I search the history of my own posts?

I was just looking for an old Facebook post I thought I made about five months ago, and scrolling through the history of my posts was very painful. It seems like Facebook gets slower and slower the farther back in your history that you go. This made me wonder, is there any way to easily search through the history of my past Facebook posts?

While it’s not obvious, the answer is that yes, you can search your Facebook posts history, and here’s how you do it, at least as of February, 2016.

How to search a MongoDB collection with Scala and Casbah

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 16.5, “How to search a MongoDB collection with Scala and Casbah.”

Problem

You want to find objects in your MongoDB collection using Scala and the Casbah driver.

Solution

Use the find* methods of the MongoCollection class to get the elements you want, specifically the find and findOne methods.

Solution to sed error message: “\1 not defined in the RE”

As a quick sed solution, if you get this “\1 not defined in the RE” error message when running a sed script:

$ sed -f sed.cmds c4.in.html > c4.out.html
sed: 2: sed.cmds: \1 not defined in the RE

the problem probably isn’t too bad. For me I usually get the error message when I forget to “escape” parentheses that I use in my search pattern. I usually write this, which is an error:

s/foo(.*)bar/\1/

when I need to write that sed command like this:

Mac OS X: Unix sed commands I use to clean MacDown HTML output

FWIW, this is the source code for a sed script I use on my Mac OS X system to convert HTML output generated by MacDown into a format I need. MacDown generates some extra “cruft” that I don’t need, so I use these sed commands to clean up that HTML output:

Scala: How to extract parts of a string that match a regex

Scala String FAQ: How can I extract one or more parts of a string that match the regular-expression patterns I specify?

Solution

Define the regular-expression patterns you want to extract from your String, placing parentheses around them so you can extract them as “regular-expression groups.” First, define the desired pattern:

val pattern = "([0-9]+) ([A-Za-z]+)".r

Next, extract the regex groups from the target string:

How to replace regular expression patterns in strings in Scala

Scala String FAQ: How do I replace a regular expression (regex) pattern in a String in Scala?

Solution

Because a String is immutable, you can’t perform find-and-replace operations directly on it, but you can create a new String that contains the replaced contents. There are several ways to do this.

You can call replaceAll on a String, remembering to assign the result to a new variable:

Scala: Deeper XML parsing, and extracting XML tag attributes alvin February 20, 2014 - 3:42pm

Problem: You need to perform deep XML searches, combining the \ and \\ methods, and possibly searching directly for tag attributes.

Solution

Combine the \\ and \ methods as needed to search the XML. When you need to extract tag attributes, place an @ character before the attribute name.

Given this simplified version of the Yahoo Weather RSS Feed: