Friday, December 19, 2014

Creating an a4 with rulers to test the output accuracy of a printer

I recently bought a new printer and wanted to test its borderless printing capabilities. So I wrote a small VBA script that creates an A4 MS Word document with four rulers, each starting at one of the four borders:
.

In case you're interested in the source code, I put in on github. The PDF can be downloaded from here (http://renenyffenegger.ch/blog/files/a4-ruler.pdf).

Tuesday, December 16, 2014

The missing \b regular expression special character in Oracle.

Oracle's regular expressions don't support the special regular expression character \b, at least not in Oracle 11i.

Consider the following table:

create table tq84_word_boundaries ( txt varchar2(50) ); insert into tq84_word_boundaries values ('AFooABarAndABaz' ); insert into tq84_word_boundaries values ('A FooA BarAndABaz' ); insert into tq84_word_boundaries values ('A Foo, a Bar and a Baz'); insert into tq84_word_boundaries values ('A Foo without a Baz' ); insert into tq84_word_boundaries values ('Foo Bar, Baz' ); insert into tq84_word_boundaries values ('Is it a Foo?' ); insert into tq84_word_boundaries values ('Bar-Foo-Baz' );

Now, I want to find all records that contain the exact word Foo. That is, I want, for example A Foo without a Baz (the fourth record), but I don't want A FooA BarAndABaz (the second record) because FooA is not the exact word Foo

If Oracle supported \b I could use

select * from tq84_word_boundaries where regexp_like(txt, '\bFoo\b');
Yet, Oracle, does not support it and no record is returned.

To improve matters, I could try

select * from tq84_word_boundaries where regexp_like(txt, '\sFoo\s');
This approach returns
TXT -------------------------------------------------- A Foo without a Baz

A bit better, but far from perfect. For example, the fifth record (Foo Bar, Baz) is not returned, because the \s doesn't match start of line. So, I improve the where condition:

select * from tq84_word_boundaries where regexp_like(txt, '(^|\s)Foo($|\s)');
The record is now returned:
TXT -------------------------------------------------- A Foo without a Baz Foo Bar, Baz

Yet again, this is far from perfect. I need also record records where Foo is followed or lead by a non word character (such as ? or -):

select * from tq84_word_boundaries where regexp_like(txt, '(^|\s|\W)Foo($|\s|\W)');
This returns
TXT -------------------------------------------------- A Foo, a Bar and a Baz A Foo without a Baz Foo Bar, Baz Is it a Foo? Bar-Foo-Baz
I think I can live with it.
Source code on github

Monday, December 15, 2014

Little things that make live easier #6: Using clip.exe in cmd.exe to copy somehting into the clipboard

Windows comes with a handy little program, named clip.exe, that can be used to quickly copy something into the clipboard from cmd.exe.

For example, if I wanted to copy the current working directory into the clipboard, I'd go

C:\some\long\and\complicated\path>echo %CD% | clip

Similarly, if I needed to copy all files within the current directory, I'd do

C:\some\path>dir /b | clip

Also, I can copy the content of a file into the clipboard like so:

C:\some\path>clip < someFile.txt

Friday, December 12, 2014

Paginating SQL queries with Oracle 12c

Oracle 12c not only allows to create TOP N queries with an understandable syntax, the row limiting clause also makes it possible to paginate a result set:

create table tq84_table ( id number, txt varchar2(10) ); insert into tq84_table values ( 2, 'two' ); insert into tq84_table values ( 1, 'one' ); insert into tq84_table values ( 6, 'six' ); insert into tq84_table values ( 8, 'eight'); insert into tq84_table values ( 3, 'three'); insert into tq84_table values ( 7, 'seven'); insert into tq84_table values ( 4, 'four' ); insert into tq84_table values ( 9, 'nine' ); insert into tq84_table values (10, 'ten' ); insert into tq84_table values ( 5, 'five' );

select id, txt from tq84_table order by id offset 4 rows -- skip first 4 records fetch next 3 rows only -- fetch next 3 records ;
returns
ID TXT ---------- ---------- 5 five 6 six 7 seven
SQL statement on github

TOP N select queries with Oracle 12c

With Oracle 12c, it's possible, finally, to do TOP n select queries with an easily understandable syntax.

Here's a table to demonstrate it:

create table tq84_table ( id number, txt varchar2(10) ); insert into tq84_table values ( 2, 'two' ); insert into tq84_table values ( 1, 'one' ); insert into tq84_table values ( 6, 'six' ); insert into tq84_table values ( 8, 'eight'); insert into tq84_table values ( 3, 'three'); insert into tq84_table values ( 7, 'seven'); insert into tq84_table values ( 4, 'four' ); insert into tq84_table values ( 0, 'zero' ); insert into tq84_table values ( 9, 'nine' ); insert into tq84_table values (10, 'ten' ); insert into tq84_table values ( 5, 'five' ); commit;

Now, a select statement with fetch first row only:

select id, txt from tq84_table order by id fetch first row only;
selects
ID TXT ---------- ---------- 0 zero
Paginating SQL queries with Oracle 12c

SQL on github

Thursday, December 11, 2014

Little things that make live easier #5: Navigation the Windows Registry with Powershell

It's possible to navigate the Windows Registry with Powershell

PS C:\> cd HKLM:/SOFTWARE/Microsoft/PowerShell PS HKLM:\SOFTWARE\Microsoft\PowerShell> ls Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell SKC VC Name Property --- -- ---- -------- 4 2 1 {Install, PID} PS HKLM:\SOFTWARE\Microsoft\PowerShell> cd 1 PS HKLM:\SOFTWARE\Microsoft\PowerShell\1> ls Hive: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1 SKC VC Name Property --- -- ---- -------- 0 1 0407 {Install} 0 6 PowerShellEngine {ApplicationBase, PSCompatibleVersion, RuntimeVersion, ConsoleHostAssemblyName...} 1 0 PSConfigurationProviders {} 2 0 ShellIds {}

Monday, December 8, 2014

Little things that make live easier #4: writing colored text in cmd.exe

A constant source of frustration when using cmd.exe is that it seems impossible to write colored text easily, unlike other shells (although I don't consider cmd.exe to be a shell). Yet, if powershell is installed, then it's possible to write such colored text. Here's a powershell script (c:\temp\colors.ps1):
write-host "" write-host " " -noNewLine write-host "Warning" -foregroundColor red -backgroundColor yellow write-host " " -noNewLine write-host "-------" -foregroundColor blue write-host " Wearing headphones doesn't" -foregroundColor red write-host " make farts silent." -foregroundColor red

When executed with something like

powershell -executionpolicy bypass c:/temp/colors.ps1

It prints