Archive for October, 2013

Interesting – Print certain lines in reverse (awk)

October 31, 2013 Leave a comment

You would like to print certain lines in a file in reverse.

awk ‘{if (NR % 2) {print} else {r=”rev”; print | r; close(r);}; }’ myfile

$less myfile
$awk '{if (NR % 2) {print} else {r="rev"; print | r; close(r);}; }' myfile

Above awk prints all even lines in from file in reverse.

NR % 2 – Returns 0 for even lines which is false. So odd lines which are 1 == true, are simply printed.
rev – bash command that reverses a line. (man rev)
close(r) – Need to explicitly close the pipped command so that the output can be flushed.
r=”rev” ; print | r; close(r);}; – Print the line, pass it to rev to reverse it, flush output so that results can be printed immediately.

More on close()


The argument filename or command can be any expression. Its value must exactly match the string that was used to open the file or start the command (spaces and other “irrelevant” characters included). For example, if you open a pipe with this:

“sort -r names” | getline foo

then you must close it with this:

close(“sort -r names”)

Here are some reasons why you might need to close an output file:

To write a file and read it back later on in the same awk program. Close the file when you are finished writing it; then you can start reading it with getline.

To write numerous files, successively, in the same awk program. If you don’t close the files, eventually you may exceed a system limit on the number of open files in one process. So close each one when you are finished writing it.

To make a command finish. When you redirect output through a pipe, the command reading the pipe normally continues to try to read input as long as the pipe is open. Often this means the command cannot really do its work until the pipe is closed. For example, if you redirect output to the mail program, the message is not actually sent until the pipe is closed.

To run the same program a second time, with the same arguments. This is not the same thing as giving more input to the first run! For example, suppose you pipe output to the mail program. If you output several lines redirected to this pipe without closing it, they make a single message of several lines. By contrast, if you close the pipe after each line of output, then each line makes a separate message.


Categories: awk, Interesting

Interesting – Parameter Expansion (bash)

October 31, 2013 Leave a comment
Categories: bash, Interesting

Installing cx_Oracle

October 25, 2013 4 comments

I needed to use python with oracle.
Before the installation, this is what I get.

>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cx_Oracle

Follow the steps below.
Download version 11 of Oracle instantclient (as the cx_Oracle 5.1.2 in this article will not work with an oracle version past 11g. Both x86 and x64 will work.)

Installination Steps.
1. Download oracle instant client from (You need to register on the site first.)
Get the following files

Extract them all into the same folder.

2. Make sure you have the following packages installed.
(I use synaptic to install these as I am on debian 6)


3. Set ORACLE_HOME variable in your .profile file.

$ cd ~
$ vim .profile

# Add the following to the end of the file. Change the path to point to the directory where you unzipped the files you downloaded in step 1 above.

export ORACLE_HOME="/usr/local/oracle/instantclient_11_2"

4. Create a soft link to NB. Do this for a different terminal so that you can read the new changes in .profile. (Or source .profile YMMV.)

$ cd ~
$ ln -s

5. As root, update ldconfig

$ su 
root> cd /etc/

Add the following line to oracle.conf. Create the file if it does not exist.

root> vim oracle.conf

Once done run the following command.

root> ldconfig

6. Download cx_Oracle from sourceforge
Please note that version 5.1.2 does not as yet support the latest version of instantclient. So make sure that you download version 11 not 12. You can tell which versions are supported by reading and looking for filesToCheck. Change directory to where you extracted the files to.
As root.

root> cd cx_Oracle-5.1.2/
root> python build
root> python install

Update (2014.Dec.06)
If you get the following error when you try “python build”
Traceback (most recent call last):
File "", line 132, in
raise DistutilsSetupError("cannot locate an Oracle software " \
distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation

It is because it is not able to find $ORACLE_HOME
Switch to another terminal

$source .profile
$cd cx_Oracle-5.1.2/

Then as root try to build and install again.

7. Check your install.

$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> print cx_Oracle.version

Update (2014.Nov.25)

Followed the same instructions to install on Linux x64.

$ python
Python 2.7.8 (default, Sep  9 2014, 22:08:43) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> print cx_Oracle.version
Categories: oracle, python Tags: ,