NewFiveFour | Blog | Portfolio


Postgresql: Remove punctuation from field with a regex

Sometimes it’s useful to remove all punctuation from a field.

Take the sentence It's done - and dusted, finally. & that's that.. We’ll use regexp_replace on this.

select regexp_replace(lower($$It's done - and dusted, finally. & that's that. $$),$$\sand\s|[&\.,\s'-]$$,$$$$,$$g$$)

That results in itsdonedustedfinallythatsthat. We’ve also removed the word and.

If we use It's done--and dusted, finally. & that's that. then the \sand\s matcher would fail. It’s an exercise to the reader to work a way around that.

We’ve also not dealt with $ and the like, but that’s also facile to remove using the above.

postgresql

Using AWK with CSV files with commas inbetween quotation marks

Sometimes you’ll get a CSV like: Here is something, And another thing, "OH LOOK, A COMMA WITHIN QUOTATION MARKS", something else.

This is annoying, since a normal awk separator like -F , will not work. But in modern version of awk, you can use -FPAT to use a regular expression.

Use awk -vFPAT='[^,]*|"[^"]*"'. This says you’re either looking for a field that ends in a comma, or looking for anything that begins and ends with quotation marks.

awk csv

Linux: How to take screenshots and screen grabs from the command line

If you have gnome-screenshot installed, you can type gnome-screenshot -a which will screenshot an area of the window you chose.

The parameters -w copies the whole window. Without parameters and it will copy the entire window.

It will be saved into your Pictures directory as Screenshot.... If you use the -c command it’s suppose to copy to the clipboard, but there’s a bug that prevents this apparently.

If you use the -i command, you’ll see a dialog with capture options.

And after the capture, you will see a save box. With this method you will be able to copy to the clipboard with the button provided.

Ensure you’re on the window you want to capture, if you use multiple desktops.

unix

Bash command line movement and deletion shortcuts

If you have typed in

Man I was mean, but I'm changing my scene

And then press alt shift b (the shift may not be needed) twice the cursor will go back to the m of my.

(alt shift f moves in the opposite direction, incidentally)

Press alt shift d it will delete the word my. Press it again and it will then delete scene.

Press ctl d and you will delete not the word but a single character under the cursor.

Press ctl k and you will delete everything after the position. ctl u everything before.

Press ctl _ and you will undo your command line edits.

unix bash

CSS: Bottom sheet using flexbox and css transition

We’ll create two divs. The first one will have a lot of content. The second will have little.

When you click on the second it will animate itself to take up most of the space.

Here is the basic layout:

<div class="main" style="height: 100%;">
  <div id="top" class="big" style="background-color: blue; overflow: hidden;">
    <div>I read</div>
    <div>the news</div>
    <div>today. </div>
    <div>Oh boy.</div>
    <div>A thou-</div>
    <div>-sand holes</div>
    <div>in Black-</div>
    <div>-burn Lan-</div>
    <div>-ca shire.</div>
  </div>
  <div id="bottomsheet" class="small" style="background-color: orange; overflow: hidden;">
    Bottom sheet
  </div>
</div>

The divs have overflow: hidden css attributes so we can resize their height regardless of their content.

Let’s now give our div a flexbox display, with the column direction. The big and small css classes will say that the first div will take up 80% and the second will take up 20%:

<style>
  .main {
    display: flex;
    flex-direction: column;
  }
  .small {
    flex-basis: 20%;
  }
  .big {
    flex-basis: 80%;
  }
</style>

We’ll now have a 80/20 height ratio between our two divs. Now let’s use the javascript (which you must put after your body’s content) to swap those dimensions via swapping the css big and small classes:

<script>
  document.querySelector("#bottomsheet").addEventListener("click", function() { 
    var small = document.querySelector(".small").classList;
    var big = document.querySelector(".big").classList;
    small.remove("small")
    small.add("big")
    big.remove("big")
    big.add("small")
  });
</script>

This all works wonderfully, but there’s no smooth animation. Let’s fix that by saying both the big and small css classes will involve a ease-in-out transition on the flex-basis attribute that will take 0.3 seconds:

.small, .big {
  transition-timing-function: ease-in-out;
  transition: flex-basis 0.3s;
}

And voila.

css

Page 1 of 77
Next