Paul - The Programmer

simple & stupid

Alter the word order in lines by VIM regular expression

Somehow, I am required to alter the order of word in lines in a text file.

For example, the input text file is like below.

hello world
hello paul
this  is
for   test

I want this file be updated was below.

world hello
paul hello
is  this
test   for

Thanks to regular expression, I do not need to update the lines one after another.

The vim regular expression is a little different than the Perl's. But most of syntax are similar.

\w  : word charaters, includes both alphabet and number characters.

\s   : space characters.

*   : match zero or more preceding character or meta character like \s, \w.

The patterns can be grouped by enclosing with \( and \). 

& :  the whole matched pattern

\0:   the whole matched pattern

\1:   the matched pattern in the first pair of \( and \)

\2:   the matched pattern in the second pair of \( and \)

So, to accomplish the above task, the substitution regular expression is :

:%s/\(\w*\)\(\s*\)\(\w*\)/\3\2\1/

 

The proper way to access to file in resource by Java

The below code demonstrates how to access to a resource file.

import java.io.*;

public class FileTest{
    public void test() throws IOException {
        String filePath = this.getClass().getResource("/test.txt").getPath();
        System.out.println( filePath );
        System.out.println( file.exists() );
    }

    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

Try to execute:

~/lab/test$ java FileTest 
/home/paul/lab/test/test.txt
true

It seems work perfectly. But this code is acturally not 100% right. When the class file is executed in a directory which name contains space character, the resource file will not be found.

~/lab/my test$ java FileTest 
/home/paul/lab/my%20test/test.txt
false

The space character in the file path was replaced by the '%20'. So, I update the path string before use it to create the File object.

import java.io.*;

public class FileTest{
    public void test() throws IOException {
        String filePath = this.getClass().getResource("/test.txt").getPath();
        System.out.println( filePath.replace("%20", " ") );
        File file = new File(filePath.replace("%20", " "));
        System.out.println( file.exists() );
    }
    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

Try this again:

~/lab/my test$ java FileTest 
/home/paul/lab/my test/test.txt
true

Looks like it's all right this time. Even the space charater in pah is supported now.

But this is still not the best way.

The above code acturally use the URL to access to the file. But the best way is use URI to access to file. Only the URI can directly support both the space and Chinese characters in file path.

import java.io.*;

public class FileTest{
    public void test() throws IOException, URISyntaxException {
        System.out.println( this.getClass().getResource("/test.txt").toURI( ).getPath());
        File file = new File(this.getClass().getResource("/test.txt").toURI());
        System.out.println( file.exists() );
    }
    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

The execution result:

~/lab/my test$ java FileTest
/home/paul/lab/my test/test.txt
true 

It works properly and the code is more simple with the URI.

The above code does not support to access to the files in another Jar file's resouce. But I'd like to talk about this topic in another post.  ;-)

Setup a Linux PyS60 development environment

Well, I know that symbian is somehow out of fashion nowadays. Since Nokia just announced their mobile phone will move to windows mobile weeks before. 

But since I just have a 5230 on my hand. I'd like to get a little bit fun from this new toy.

The S60 SDK is quite heavy and windows only. The PyS60 is the only choice left for me.

First thing first. The python runtime is needed on the phone itself. 

The latest sis package is available on the source forge http://sourceforge.net/projects/pys60/files/pys60/1.4.5/

I just download the S603rd edition sis packages which should support the S60 5th on 5230. Install the python runtim and python shell by the pc suite.

Now python shell is ready on the phone. There is one useful feature that you can remote connect to the python console via bluetooth.

But debug on the phone is not so convenient. The project PyS60 emulation library supports run the S60 python script in a general python runtime environment.

The needed libraries can be found on http://sourceforge.net/projects/pys60-compat/files/pys60-compat/.

Unzip the python libraries and set the PYTHONPATH accordingly. I can run the S60 python script on my laptop.

Let's see what I can do with the PyS60. Maybe try to create a tic-toc-toe for fun.

Maybe I should buy a bluetooth adapter for my old laptop. Then I can connnect to the python console via bluetooth with the minicom. This should be interesting.

I will update you with my findings. ;-)

Convert file format from dos to unix

Convert file format by sed

$cp you-file-name your-file-name.bak
$sed 's/^M//g' your-file-name.bak > your-file-name

Covert file format by Perl

$perl -i.bak -pe 's/^M//g;'  your-file-name

Covert file format by vim

:setlocal ff=unix
:wq

 

Detect file format is dos or unix

Detect file format with grep.

$ grep '^M'  your-file-name

^M is Ctrl-V + Ctrl-M. If the grep returns any line, the file is in DOS format.

Detect file format with vim.

1) Open the file with vim.

2) Use the vim set command to show the file format.

:set ff?

The command returns fileformat=<dos/unix/mac> to indect current file format.