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

Difference between TRIMSPOOL and TRIMOUT

SET TRIMSPOOL ON
Determines whether SQL*Plus allows trailing blanks at the end of each
spooled line. ON removes blanks at the end of each line, which may
improve performance especially when you access SQL*Plus from a slow
communications device. TRIMSPOOL ON does not affect terminal output.
SET TRIMOUT ON
Determines whether SQL*Plus allows trailing blanks at the end of each
displayed line. ON removes blanks at the end of each line, which may
improve performance especially when you access SQL*Plus from a slow
communications device. TRIMOUT ON does not affect spooled output.

Tuesday, October 21, 2008

How to delete many files ending with an extension?

Some times you cannot rely on ‘rm’ command for deleting huge number of files. Even ‘-f’ switch will fail you. In those situations the following command can be used.

find . -name '*.ext' | xargs rm

How to edit a read-only file (eg. /etc/hosts)

You do not need to change the access rights in /etc/inet/hosts
Logged in as root, the file is read only, but since you are root, you can make changes to the file. Just make sure when you get done editing the file (yes it shows up as read only, but don’t let that fool you)
you type
:wq!
You are forcing an over write of the file.
Remember as root/su you have rights to everything, so make sure that you do not make a mistake. Always make a backup of what you are working on before you make the changes.

How to remove last line of a large file

I need to remove last line of a file that is very very huge.

sed '$d' infile > outfile

copied from here