2011-04-16

Running Batch Files in Vim Ex-Mode

To modify a file using vim in ex-mode, you should be able to do this: vim -esn < modify.ex File.txt. The -esn option starts vim in ex-mode, a non-interactive session and no backup is created. The modify.ex file can contain a sequence of editing commands, ending with wq to write and close the file. However, in a Windows cmd session, the editing commands in modify.ex are executed but vim doesn't exit.

My workaround is to run a command (-c option) which uses vim's source command to run modify.ex: vim -esn -c "source modify.ex" File.txt.

2011-03-26

Configure NetBeans to Statically Link MinGW C & C++ Libraries

Installed MinGW g++ 4.5.2 and got the following Windows error when trying to run a C++ program I just compiled: The program can't start because libgcc_s_dw2-1.dll is missing from your computer. After fixing that error, Windows displayed a similar message regarding libstdc++-6.dll. Basically, g++ doesn not statically link the C and C++ standard libraries with an executable by default.

  • In Windows, copy the required library files, libgcc_s_dw2-1.dll and libstdc++-6.dll from the MinGW bin folder into the same folder as the executable or add the MinGW bin folder into the PATH environment variable.
  • If using NetBeans 6.9, configure the project properties to statically link the libraries to the executable:
    1. Select menu item File, Project Properties.
    2. In the Project Properties dialog, expand node Build, Linker.
    3. In Command Line, set Additional Options = -static-libgcc -static-libstdc++

2011-03-05

Leading Semicolon Comments a Line in CMD FOR /F

I wondered why the following CMD script was not processing some lines ...

for /f %a in (test.txt) do @echo %a

... with this sort of test where the semicolon is a field separator. All the lines were processed when the field separator is a space, tab or comma.

x;y;z
;y;z

When I ran the script, the echo command is only called for the first line. It turns out that, by default, for /f regards any line starting with a semicolon to be a comment and ignores it. It's similar to using:

for /f "eol=;" %a in (test.txt) do @echo %a

Note that the Microsoft documentation doesn't state the default for the eol keyword.

See Also

2011-02-12

Negate Numbers as String in Gawk

I had to negate numbers in a data file. My initial script simply negated the required field. Here a test:

gawk "{ print -$1 }"
123
-123
123.45678
-123.457

I wanted to preserve all the digits after the decimal point so I tried printf() (the multiple double-quotes is to escape double-quotes in Windows CMD):

gawk "{ printf("""%10lf\n""", -$1) }"
123
-123.000000
123.45678
-123.456780
123.456
-123.456000

Hm, now the result was being padded to the right depending on the number of digits specified. Since I only wanted to negate the values and not do any arithmetic, a trick is to add or delete the leading minus sign from the input string depending whether the input value was positive or negative, respectively:

gawk "{ print $1<0 ? substr($1, 2) : """-""" $1 }"
123
-123
123.45678
-123.45678
-123
123
-123.45678
123.45678