Friday, February 27, 2009

The rev command

rev command is used to print the lines of a file in reverse order.
i used it with cut to cut the last column of a file . I used it
because a column in the begining used special character in it

for eg:
Irregular line
"Buongiorno","BUSA, Inc",11222,"ALT",2325,22318.75
regular line
"Buongiorno","BUSA",11222,"ALT",2325,22318.75
the comma after BUSA gives wrong column

awk command to sum a column

bbs-list file

aardvark 555-5553 1200/300 B
alpo-net 555-3412 2400/1200/300 A
barfly 555-7685 1200/300 A
bites 555-1675 2400/1200/300 A
camelot 555-0542 300 C
core 555-2912 1200/300 C
fooey 555-1234 2400/1200/300 B
foot 555-6699 1200/300 B
macfoo 555-6480 1200/300 A
sdace 555-3430 2400/1200/300 A
sabafoo 555-2127 1200/300 C

awk command for sum of a column

cat bbs-list | cut -f3 -d" "|cut -f1 -d"/"| awk '{ s=$0+s} END {print s}'

SPECIFYING A FIELD SEPARATOR IN AWk

awk -F: ’{ print $1 }’ /etc/passwd | sort

Monday, February 23, 2009

Print the Stack Trace of the Exception to a String

When an exception occurs, this code snippet will print the stack trace of the exception to a string. This way, the exception stack trace at runtime can be recorded in log file.


import java.io.PrintWriter;
import java.io.StringWriter;
public static String getStackTrace(Throwable t)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
Taken from Devx
Sujata De

Friday, February 13, 2009

Linux / Unix / Solaris date command

Using perl Command line
ls -1 | perl -n -e 'chop; print localtime( (stat($_))[9] ) . "\n";

The perl command processes each line of input (with -n option) and for each line, $_ is the input line. "chop" removes the newline character at the end of the line. "stat" retrieves the inode data from the filename (in $_). "localtime" will spit out the date/time of the file using the local timezone. (Use "gmtime" if you want it to ignore timezone settings.)

Log style date command
date '+ %b %e %T'


YYYY-MM-DD

echo `date +"%Y-%m-%d"`


Result:

2008-05-29


YYYY-MM-DD_HH_MM_SS

echo `date +"%Y-%m-%d_%H-%M-%S"`


Result:

2008-05-29_10-35-44

You can use these for creating files names of backups.

Taken from following linkdate command
Another good date link