output

Unix: get the file date

Thursday, March 15th, 2007

ls -1 | cpio -o | cpio -ivt | awk ‘{print $NF, $(NF-1), $(NF-4), $(NF-3) }’
Warning: I/O expensive for the large files!
Perl:
@a = localtime((stat($my_file))[9]); $a[4]++;
printf “%02d%02d%02d”,@a[5,4,3];

Oracle: transpose data table (rows into columns)

Wednesday, February 28th, 2007

create table MAIN_TBL ( magazine varchar2(10), region varchar2(5), quantity int );
insert into MAIN_TBL values ( ‘Playboy’, ‘Nord’, 1 );
insert into MAIN_TBL values ( ‘Playboy’, ‘East’, 2 );
insert into MAIN_TBL values ( ‘AutoWeek’, ‘Nord’, 3 );
insert into MAIN_TBL values ( ‘AutoWeek’, ‘West’, 4 );
insert into MAIN_TBL values ( ‘Wired’, ‘Nord’, 5 [...]

Write to the alert log from PL/SQL code

Monday, February 19th, 2007

The following  undocumented function could be used to write to the alert log:
execute sys.dbms_system.ksdwrt(code, message);
Parameters:
1 - Write to trace file.
2 - Write to alertlog.
3 - Write to both.
Example:
execute sys.dbms_system.ksdwrt(2,to_char(sysdate, ‘Dy Mon DD HH24:MI:SS YYYY’)||’ Hello!’ );
create or replace trigger trg_delme
BEFORE UPDATE on frodo.delme
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
begin
sys.dbms_system.ksdwrt(2,to_char(sysdate, ‘Dy Mon DD HH24:MI:SS YYYY’) );
end;

Redirect script output to the log

Wednesday, March 8th, 2006

If the whole output of the complex script should be redirected to the log, the following trick could be used.
if [ "$1" != "-log" ] ; then
$0 -log “$@” 2>&1 | tee the_log_file.$$.log
echo “The log file for the current session: the_log_file.$$.log”
exit 0
fi
shift # remove “-log” [...]

Print the PATH directories in the readable format

Thursday, February 16th, 2006

echo $PATH| awk -v RS=”:” ‘{ print $0 }’
echo $LD_LIBRARY_PATH |awk -v RS=”:” ‘{ system ( “ls -rltd ” $0 ) }’
Warning!
As far as the option ‘-v’ is used, the new awk(nawk in some systems) should be used.
To check if the new version of awk is installed:
awk 1 /dev/null
The output will be empty for new [...]

Rev function and comma-separated output

Thursday, February 2nd, 2006

Here is the shell command snippet to display comma-separated output:
ls -lrt | rev | sed ’s/\\([0-9][0-9][0-9]\\)/\\1,/g’ | rev | sed ’s/\\([\^0-9]\\),\\([0-9]\\)/\\1\\2/g;s/\^,\\([0-9]\\)/\\1/g’
Example:
-rw-r—– 1 sybase dba 1,572,872,192 Feb 2 07:09 master.dbf
Rev function (absent on SunOS) :
(Warning! Avoid spaces before #define - SunOS cc compiler doesn’t like them)

#include <stdio.h>
#ident  "@(#)rev 1.0  [...]

Keep on coding