Using sed with regex capture groups

There are many times when you have a file from which you want to extract specific strings based on a regex and using a capture group is a very efficient way to parse multiple strings from the same line.

I have found that sed is the easiest way to do so on the Linux command line.

Given the following input file:

This is a line of text with a year=2020 month=12 in it
This line of text does not have a year or month in it
This year=2021 is the current year the current month=1
This is the year=2021 the month=2

Let’s say that you want to extract the year and the month digits from each line and generate a line of output for each line if input that looks like:

my year: <year>, my month: <month>

You would run the following command defining two capture groups:

sed -rn 's/.*year=([0-9]+).*month=([0-9]+).*/my year: \1, my month: \2/p' input.txt

Which will output:

my year: 2020, my month: 12
my year: 2021, my month: 1
my year: 2021, my month: 2

The -rn flag tells sed to use extended regular expressions in the script and to suppress printing unless explicitly directed after we make a match.

The s command tells sed that we are going to execute a substitution and that we will define a regex, a replacement string, and optional flags.

.*year=([0-9]+).*month=([0-9]+).*

Defines two capture groups. One to look for any number of contiguous digits after year= and another for any number of contiguous digits after month=. The .* explicitly tells sed that we want to ignore any number of any type of characters between the defined groups.

my year: \1, my month: \2/p

Tells sed how to format the output to include each capture group, \1 for capture group 1 and \2 for capture group 2.

How to Find Ingested Foreign Objects in Poop

Admittedly, not the most savory subject. But, for those of us with pets and/or children it is sometimes a necessity to try to find a previously ingested foreign object in feces to make sure that it does not cause medical problems in the person or animal that has accidentally eaten it.

Recently, we thought our dog had eaten a relatively small magnet and figured that we would have to be checking his feces over the next few days to ensure that he passed it. I’ve not had to do this before and off the top of my head I didn’t know the best way to go about it. Turning to the Internet, all of the searches with the exact same title as this blog article turned up other articles about how long things take to move through the digestive tract. That you should call your Dr. or Vet. But nothing that specifically said how to actually search for the object in the offending matter.

Eventually, I found a blog posting on some dog owner site with a very good suggestion and here is what I did.

  1. Collect the feces as you normally would in a poop bag and bring it home.
  2. Cut open the bag and dump it in a quart sized or bigger ziplock freezer bag. The bigger the bag (to a point) the better.
  3. Shake the feces to the bottom of the bag and lay it flat on your driveway, sidewalk, or similarly hard surface outside. Doing it outside makes clean-up much easier if the bag develops a rip.
  4. Squeeze as much air out of the bag as possible while smooshing the feces out such that it is transformed into a thin layer inside the bag.
  5. While doing so, you will easily find all but the smallest of objects just by feeling it through the bag without getting your hands dirty.
  6. Once you identify it, you can either retrieve it from the bag and clean it off, or just toss it knowing that your child/pet has passed the item safely.

If you don’t like bugs, you are on the wrong planet

Something I mention to my wife now and then when she get’s irritated by some sort of insect. There’s a lot more of them than us! I stumbled upon an interesting article today that gives some information on the numbers of species and individuals on the planet.

Other than bacteria (approx 25,000,000 different species) arthropods (of which are insects, spiders, etc.) number approximately 6,000,000 species.

Another interesting fact . . . total number of arthropods on earth . . . 1 billion billion (10 to the 18) individuals.

🙂

Cool article on conservation and biodiversity.