Monday, November 3, 2008

Ceiling in awk

function ceiling(x)
{
return (x == int(x)) ? x : int(x)+1
}

Stolen from link

Rounding in awk

echo $1 | awk '
function round(aa) {
return int( aa + 0.5 )
}
{
printf("%d\n",round($1));
}'

Stolen from link

Tuesday, October 28, 2008

How to trim leading and trailing spaces in a string?

To trim leading spaces
string=`echo $string | sed -e 's/^ *//g'`
echo string=$string

To trim trailing spaces
string=`echo $string | sed -e 's/ *$//g'`
echo string=$string

Converting a string from upper case to lower case (in Unix)

curDate=`date "+%d-%h-%Y"`
echo curDate=$curDate
curDateInUpperCase=`echo $curDate | tr "[:lower:]" "[:upper:]"`
echo curDateInUpperCase=$curDateInUpperCase

Display date in 'DD-Mon-YYYY' format in unix

Command: date "+%d-%h-%Y"
Output: 28-Oct-2008

Wednesday, October 22, 2008

How to send email with attachments using mailx utility?

Here is a code stolen from orafaq.com that helps to send email with attachments using mailx utility (with slight modifications, ofcourse)
#!/bin/ksh
# -----------------------------------------------------------------------
# Filename: mailx.ksh
# Purpose: Demonstrates how one can attach files when sending E-Mail
# messages from the Unix mailx utility.
# Author: Frank Naude, Oracle FAQ
# -----------------------------------------------------------------------
SUBJECT="Send mail from Unix with file attachments"
TO="address1@domain.com address2@domain"
echo "Send the E-mail message..."
/usr/bin/mailx -s "$SUBJECT" "$TO" <<-EOF
Hi,
This sample E-mail message demonstrates how one can attach files when sending messages with the Unix mailx utility.

First attachment: mailx.ksh (this script)
Second attachment: /etc/passwd file

Best regards
your name

~< ! uuencode mailx.ksh mailx.txt
~< ! uuencode /etc/passwd passwords
~.
EOF
echo "Done!"

When given a pathname, how to delete any prefix up to the last slash ('/')?

basename is a standard UNIX computer program.
When basename is given a pathname, it will delete any prefix up to the last slash ('/') character

Usage
basename string [suffix]
string
A pathname
suffix
If specified, basename will also delete the suffix.


Example
$ basename /usr/home/jsmith/basename.wiki ki
basename.wi