Archive

Archive for October, 2013

Interesting – Print certain lines in reverse (awk)

October 31, 2013 Leave a comment

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

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

$less myfile
kitten
pig
horse
cow
donkey
$awk '{if (NR % 2) {print} else {r="rev"; print | r; close(r);}; }' myfile
kitten
gip
horse
woc
donkey

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

Note:
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()

close(filename)
or
close(command)

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.

Source:
http://www.unix.com/showthread.php?p=302869631
http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_7.html#SEC67

Advertisements
Categories: awk, Interesting

Interesting – Parameter Expansion (bash)

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

Installing cx_Oracle

October 25, 2013 3 comments

Problem:
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

Solution:
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 oracle.com http://www.oracle.com/technetwork/topics/linuxsoft-082809.html (You need to register on the site first.)
Get the following files

instantclient-basic-linux-11.2.0.4.0.zip
instantclient-sdk-linux-11.2.0.4.0.zip
instantclient-sqlplus-linux-11.2.0.4.0.zip

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)

build-essential
python-dev
libaio-dev

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"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

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

$ cd ~
$ cd $ORACLE_HOME
$ ln -s libclntsh.so.11.1 libclntsh.so

5. As root, update ldconfig

$ su 
root> cd /etc/ld.so.conf.d

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

root> vim oracle.conf
/usr/local/oracle/instantclient_11_2

Once done run the following command.

root> ldconfig

6. Download cx_Oracle from sourceforge
http://sourceforge.net/projects/cx-oracle/files/
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 setup.py and looking for filesToCheck. Change directory to where you extracted the files to.
As root.

root> cd cx_Oracle-5.1.2/
root> python setup.py build
root> python setup.py install

Update (2014.Dec.06)
If you get the following error when you try “python setup.py build”
Traceback (most recent call last):
File "setup.py", 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
5.1.2
>>> 


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
5.1.2
>>>  
Categories: oracle, python Tags: ,