Sunday, July 31, 2011

Async I/O

Below are some of the points about async i/o which was explained by tom:-

DBWR must write blocks to the datafiles and ensure they get written. These blocks are protected by redo in the oneline redo log. Before we can reuse a redo log file, we must ensure that the blocks protected by that redo are safely and totally written to disk.

DBWR can do this in one of two ways. Lets say DBWR has 100 blocks to write to disk. It could:


for i in 1 .. 100
loop
write block X to disk and wait for the OS to tell
us the operation is totally complete (the block is
not buffered by the OS or anything, it is on disk)
end loop

As you can imagine, that would be slow. For each block, we would have to wait for the OS to tell us the block is written. If there was any contention on that disk (eg: another process was reading /writeing that disk), we would have to wait event longer.


Another way is to write the block to disk and not wait for the OS to tell us the block has been written. rather, the OS will "call back" to us and tell us that block X was written. The logic might look like this:

for i in 1 .. 100
loop
write block X and return immediately, don't wait
end loop

now wait for the 100 blocks to "call back" to us, let the OS
do its work and wait.

In this case, we give the OS all of the work we want done and let it schedule it as fast as it can. While it is off doing its thing, we are submitting yet more blocks to be written. We can get some parallel processing going on here and things go faster.

When the OS does not support async IO, we can use mutliple processes to mimick it. DBWR will use the slaves to do the syncronous writes and use many of them simultaneously.

When the slaves are done, they call be to DBWR just like the OS would.

Question:-
Hi Tom,
Now i understood about DBWR SLAVE process.
another question.

Accouring to your reply DBWR process is coordinator and SALVES are the processes which are doing real work.

1)Does that mean there is always DBWR SLAVE process in the database for DBWR process?
2)How many DBWR SLAVE process we can have in database?

I know there is DBWRnnn Parameter in init.ora file.

3)MULTIPLE DBWR SALVE process will distribute dirty buffers write to disk. Am i right?

Answer:-

1) No, if your server supports asynch IO then there would be no point having slaves for the DBWR process since slaves are used to simulate asynch IO

2) There is an init ora parameter db_writer_processes which controls the number of DBWR processes.The maximum number of DBWR processes is 10, most systems
have 1 DBWR but on large multi CPU machines it is set higher.

If you change DBWR_PROCESSES you should also configure DB_BLOCK_LRU_LATCHES. This controls the number of LRU (least recently used) list latches. It is recommended
each DBWR process have it's own list.

There is a seperate parameter for SLAVES called DBWn_IO_SLAVES.

3) Yes the DBWR or it's SLAVE writes dirty blocks from the buffer cache to disk.

Multiple DBWR are more efficient then SLAVES because the buffer cache is effectively partitioned such that each database writer process maintains a disjoint set of buffers. The allows the buffer cache processing to be parallelized across the available database writer processes.

DBWR IO slaves do not parallelize buffer processing because a single DBWR is used to allocate work to the I/O slaves. IO slaves I suspect will have a higher overhead
then multiple DBWRs since there is IPC communication between the Master DBWR and it's slaves which results in context switches.

Multi DBWR I believe divide the buffer cache into equal sized groups of buffers (working sets).

The working sets are assigned to the DBWRs in a round robin fashion which ensures each receives a share of the different pools such as keep, recycle default etc.
The number of working sets is defined by the number of LRU latches.

I have noticed that a recovery after a shutdown abort is faster with a single DBWR then with multiple DBWRs and I haven't worked out why yet. There is no appreciable
difference when starting the database with multi DBWR after normal or immediate shutdown.

Since I don't work for Oracle I could be completely wrong but hopefully Tom will comment on what I have said.

-------------

Synch I/O = Blocking I/O where a process waits (does not return) till the O/S completes the I/O
Asynch I/O = Non-Blocking I/O where a process does not wait for the O/S to complete the I/O, but rather continues with its processing and the O/s sends back a signal to the process to indicate the I/O completion.

Monday, July 25, 2011

Creating ASM DISK & RAW DISK using Devices

login as: root
root@192.168.1.3's password:
Last login: Wed Jun 29 10:10:50 2011 from 192.168.1.2
[root@linux11g ~]# rpm -qa oracleasm*
oracleasm-support-2.0.3-2
oracleasm-2.6.9-67.0.0.0.1.ELsmp-2.0.3-2
[root@linux11g ~]# cd /software
[root@linux11g software]# ls -l
total 36
drwx------ 2 oracle oinstall 16384 Dec 21 2010 lost+found
-rw-r--r-- 1 root root 12945 Oct 7 2009 oracleasmlib-2.0.4-1.el4.i386.rpm
[root@linux11g software]# rm -ivh oracleasmlib-2.0.4-1.el4.i386.rpm
rm: invalid option -- h
Try `rm --help' for more information.
[root@linux11g software]# rpm -ivh oracleasmlib-2.0.4-1.el4.i386.rpm
Preparing... ########################################### [100%]
1:oracleasmlib ########################################### [100%]
[root@linux11g /]# rpm -qa oracleasm*
oracleasm-support-2.0.3-2
oracleasm-2.6.9-67.0.0.0.1.ELsmp-2.0.3-2
oracleasmlib-2.0.4-1.el4
[root@linux11g ~]# fdisk

Usage: fdisk [-l] [-b SSZ] [-u] device
E.g.: fdisk /dev/hda (for the first IDE disk)
or: fdisk /dev/sdc (for the third SCSI disk)
or: fdisk /dev/eda (for the first PS/2 ESDI drive)
or: fdisk /dev/rd/c0d0 or: fdisk /dev/ida/c0d0 (for RAID devices)
...
[root@linux11g ~]# fdisk -l

Disk /dev/sda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 702 5116702+ 83 Linux
/dev/sda3 703 963 2096482+ 83 Linux
/dev/sda4 964 3263 18474750 5 Extended
/dev/sda5 964 1224 2096451 82 Linux swap
/dev/sda6 1225 1289 522081 83 Linux
/dev/sda7 1290 3263 15856123+ 83 Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdc doesn't contain a valid partition table


[root@linux11g ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-652, default 652): 2684M
Value out of range.
Last cylinder or +size or +sizeM or +sizeK (1-652, default 652): +2684M

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@linux11g ~]# fdisk /dev/sdb

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (328-652, default 328):
Using default value 328
Last cylinder or +size or +sizeM or +sizeK (328-652, default 652): +2684M
Value out of range.
Last cylinder or +size or +sizeM or +sizeK (328-652, default 652): +2500M

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@linux11g ~]# fdisk -l

Disk /dev/sda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 702 5116702+ 83 Linux
/dev/sda3 703 963 2096482+ 83 Linux
/dev/sda4 964 3263 18474750 5 Extended
/dev/sda5 964 1224 2096451 82 Linux swap
/dev/sda6 1225 1289 522081 83 Linux
/dev/sda7 1290 3263 15856123+ 83 Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 327 2626596 83 Linux
/dev/sdb2 328 632 2449912+ 83 Linux

Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdc doesn't contain a valid partition table
[root@linux11g ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-261, default 261): +500M

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (63-261, default 63):
Using default value 63
Last cylinder or +size or +sizeM or +sizeK (63-261, default 261): +500M

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (125-261, default 125):
Using default value 125
Last cylinder or +size or +sizeM or +sizeK (125-261, default 261): +500M

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (187-261, default 187):
Using default value 187
Last cylinder or +size or +sizeM or +sizeK (187-261, default 261):
Using default value 261

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@linux11g ~]# fdisk -l

Disk /dev/sda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 702 5116702+ 83 Linux
/dev/sda3 703 963 2096482+ 83 Linux
/dev/sda4 964 3263 18474750 5 Extended
/dev/sda5 964 1224 2096451 82 Linux swap
/dev/sda6 1225 1289 522081 83 Linux
/dev/sda7 1290 3263 15856123+ 83 Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 327 2626596 83 Linux
/dev/sdb2 328 632 2449912+ 83 Linux

Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 62 497983+ 83 Linux
/dev/sdc2 63 124 498015 83 Linux
/dev/sdc3 125 186 498015 83 Linux
/dev/sdc4 187 261 602437+ 83 Linux
[root@linux11g ~]# /etc/init.d/oracleasm configure
Configuring the Oracle ASM library driver.

This will configure the on-boot properties of the Oracle ASM library
driver. The following questions will determine whether the driver is
loaded on boot and what permissions it will have. The current values
will be shown in brackets ('[]'). Hitting without typing an
answer will keep that current value. Ctrl-C will abort.

Default user to own the driver interface []: oracle
Default group to own the driver interface []: oinstall
Start Oracle ASM library driver on boot (y/n) [n]: y
Fix permissions of Oracle ASM disks on boot (y/n) [y]: y
Writing Oracle ASM library driver configuration: [ OK ]
Loading module "oracleasm": [ OK ]
Mounting ASMlib driver filesystem: [ OK ]
Scanning system for ASM disks: [ OK ]
[root@linux11g ~]# /etc/init.d/oracleasm createdisk VOL1 /dev/sdb1
Marking disk "/dev/sdb1" as an ASM disk: [ OK ]
[root@linux11g ~]# /etc/init.d/oracleasm createdisk VOL2 /dev/sdb2
Marking disk "/dev/sdb2" as an ASM disk: [ OK ]
[root@linux11g ~]# /etc/init.d/oracleasm scandisks
Scanning system for ASM disks: [ OK ]
[root@linux11g ~]# /etc/init.d/oracleasm listdisks
VOL1
VOL2
[root@linux11g ~]# raw /dev/sdc1
raw device '/dev/sdc1' is not a character dev
[root@linux11g ~]# raw /dev/raw/raw1 /dev/sdc1
/dev/raw/raw1: bound to major 8, minor 33
[root@linux11g ~]# raw /dev/raw/raw2 /dev/sdc2
/dev/raw/raw2: bound to major 8, minor 34
[root@linux11g ~]# raw /dev/raw/raw3 /dev/sdc3
/dev/raw/raw3: bound to major 8, minor 35
[root@linux11g ~]# raw /dev/raw/raw4 /dev/sdc4
/dev/raw/raw4: bound to major 8, minor 36
[root@linux11g ~]# vi /etc/sysconfig/rawdevices
[root@linux11g ~]# vi /etc/hosts
[root@linux11g ~]# cd $ORACLE_HOME
[root@linux11g ~]# pwd
/root
[root@linux11g ~]# cd /u01/app/oracle/product/11.1.0/db_1/bin/localconfig add
-bash: cd: /u01/app/oracle/product/11.1.0/db_1/bin/localconfig: Not a directory
[root@linux11g ~]# /u01/app/oracle/product/11.1.0/db_1/bin/localconfig add
/etc/oracle does not exist. Creating it now.
Successfully accumulated necessary OCR keys.
Creating OCR keys for user 'root', privgrp 'root'..
Operation successful.
Configuration for local CSS has been initialized

Cleaning up Network socket directories
Setting up Network socket directories
Adding to inittab
Startup will be queued to init within 30 seconds.
Checking the status of new Oracle init process...
Expecting the CRS daemons to be up within 600 seconds.
Cluster Synchronization Services is active on these nodes.
linux11g
Cluster Synchronization Services is active on all the nodes.
Oracle CSS service is installed and running under init(1M)
[root@linux11g ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 4.9G 4.0G 616M 87% /
/dev/sda1 494M 17M 453M 4% /boot
none 506M 254M 252M 51% /dev/shm
/dev/sda3 2.0G 36M 1.9G 2% /software
/dev/sda6 494M 11M 458M 3% /tmp
/dev/sda7 15G 13G 1.3G 92% /u01
[root@linux11g ~]# umount tmpfs
umount: tmpfs: not found
[root@linux11g ~]# df -k /dev/shm
Filesystem 1K-blocks Used Available Use% Mounted on
none 517280 259444 257836 51% /dev/shm
[root@linux11g ~]# umount none
[root@linux11g ~]# mount -t tmpfs shmfs -o size=1200m /dev/shm
[root@linux11g ~]# df -k /dev/shm
Filesystem 1K-blocks Used Available Use% Mounted on
none 1228800 0 1228800 0% /dev/shm
[root@linux11g ~]# vi /etc/fstab
Add the below in fstab file:-

tmpfs /dev/shm tmpfs size=1200m 0 0

[root@linux11g etc]# chown oracle:dba /dev/raw/raw[1-4]
[root@linux11g etc]# ls -l /dev/raw*
crw------- 1 root root 162, 0 Jul 26 2011 /dev/rawctl

/dev/raw:
total 0
crw-rw---- 1 oracle dba 162, 1 Jul 26 08:32 raw1
crw-rw---- 1 oracle dba 162, 2 Jul 26 08:32 raw2
crw-rw---- 1 oracle dba 162, 3 Jul 26 08:32 raw3
crw-rw---- 1 oracle dba 162, 4 Jul 26 08:32 raw4
[root@linux11g etc]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 4.9G 4.0G 616M 87% /
/dev/sda1 494M 17M 453M 4% /boot
none 1.2G 410M 791M 35% /dev/shm
/dev/sda3 2.0G 36M 1.9G 2% /software
/dev/sda6 494M 11M 458M 3% /tmp
/dev/sda7 15G 13G 1.3G 92% /u01
shmfs 1.2G 410M 791M 35% /dev/shm
[root@linux11g etc]# fdisk -l

Disk /dev/sda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 702 5116702+ 83 Linux
/dev/sda3 703 963 2096482+ 83 Linux
/dev/sda4 964 3263 18474750 5 Extended
/dev/sda5 964 1224 2096451 82 Linux swap
/dev/sda6 1225 1289 522081 83 Linux
/dev/sda7 1290 3263 15856123+ 83 Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 327 2626596 83 Linux
/dev/sdb2 328 632 2449912+ 83 Linux

Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 62 497983+ 83 Linux
/dev/sdc2 63 124 498015 83 Linux
/dev/sdc3 125 186 498015 83 Linux
/dev/sdc4 187 261 602437+ 83 Linux
[root@linux11g etc]# su - oracle
[oracle@linux11g ~]$ lsnrctl start

LSNRCTL for Linux: Version 11.1.0.6.0 - Production on 26-JUL-2011 10:05:22

Copyright (c) 1991, 2007, Oracle. All rights reserved.

Starting /u01/app/oracle/product/11.1.0/db_1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.1.0.6.0 - Production
System parameter file is /u01/app/oracle/product/11.1.0/db_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/linux11g/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=linux11g.manzoor.com)(PORT=1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 11.1.0.6.0 - Production
Start Date 26-JUL-2011 10:05:22
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/11.1.0/db_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/diag/tnslsnr/linux11g/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=linux11g.manzoor.com)(PORT=1521)))
The listener supports no services
The command completed successfully

[oracle@linux11g ~]$ tnsping test11g

TNS Ping Utility for Linux: Version 11.1.0.6.0 - Production on 26-JUL-2011 10:05:39

Copyright (c) 1997, 2007, Oracle. All rights reserved.

Used parameter files:
/u01/app/oracle/product/11.1.0/db_1/network/admin/sqlnet.ora


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = linux11g.manzoor.com)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = test11g)))
OK (0 msec)
[oracle@linux11g ~]$ sqlplus sys/admin@test11g as sysdba

SQL*Plus: Release 11.1.0.6.0 - Production on Tue Jul 26 10:05:47 2011

Copyright (c) 1982, 2007, Oracle. All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


SQL> create tablespace manzoor datafile '+DATA1' size 100m;

Tablespace created.

SQL> create tablespace usingraw datafile '/dev/raw/raw3' size 400m;

Tablespace created.

SQL> drop tablespace usingraw including contents and datafiles;

Tablespace dropped.

SQL> create tablespace usingraw_1 datafile '/dev/raw/raw3' size 400m;

Tablespace created.

SQL> create tablespace usingraw_2 datafile '/dev/raw/raw4' size 400m;

Tablespace created.

SQL> exit

[oracle@linux11g ~]$ rman target /

Recovery Manager: Release 11.1.0.6.0 - Production on Tue Jul 26 10:04:43 2011

Copyright (c) 1982, 2007, Oracle. All rights reserved.

connected to target database: TEST11G (DBID=904564690)

RMAN> backup tablespace usingraw_1;

Starting backup at 26-JUL-11
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=131 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=130 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=129 device type=DISK
allocated channel: ORA_DISK_4
channel ORA_DISK_4: SID=126 device type=DISK
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00007 name=/dev/raw/raw3
channel ORA_DISK_1: starting piece 1 at 26-JUL-11
channel ORA_DISK_1: finished piece 1 at 26-JUL-11
piece handle=/u01/app/oracle/flash_recovery_area/TEST11G/backupset/2011_07_26/o1_mf_nnndf_TAG20110726T100450_72wjzlfp_.bkp tag=TAG20110726T100450 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 26-JUL-11

RMAN> sql 'alter tablespace USINGRAW_1 offline immediate';

sql statement: alter tablespace USINGRAW_1 offline immediate

RMAN> restore tablespace USINGRAW_1;

Starting restore at 26-JUL-11
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00007 to /dev/raw/raw3
channel ORA_DISK_1: reading from backup piece /u01/app/oracle/flash_recovery_area/TEST11G/backupset/2011_07_26/o1_mf_nnndf_TAG20110726T100450_72wjzlfp_.bkp
channel ORA_DISK_1: piece handle=/u01/app/oracle/flash_recovery_area/TEST11G/backupset/2011_07_26/o1_mf_nnndf_TAG20110726T100450_72wjzlfp_.bkp tag=TAG20110726T100450
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
Finished restore at 26-JUL-11


RMAN> recover tablespace USINGRAW_1;

Starting recover at 26-JUL-11
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4

starting media recovery
media recovery complete, elapsed time: 00:00:00

Finished recover at 26-JUL-11

RMAN> sql 'alter tablespace USINGRAW_1 online';

sql statement: alter tablespace USINGRAW_1 online

RMAN> backup tablespace manzoor;

Starting backup at 26-JUL-11
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00006 name=+DATA1/test11g/datafile/manzoor.256.757504713
channel ORA_DISK_1: starting piece 1 at 26-JUL-11
channel ORA_DISK_1: finished piece 1 at 26-JUL-11
piece handle=/u01/app/oracle/flash_recovery_area/TEST11G/backupset/2011_07_26/o1_mf_nnndf_TAG20110726T102018_72wkwk70_.bkp tag=TAG20110726T102018 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 26-JUL-11

RMAN> sql 'alter tablespace manzoor offline immediate';

sql statement: alter tablespace manzoor offline immediate

RMAN> restore tablespace manzoor;

Starting restore at 26-JUL-11
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00006 to +DATA1/test11g/datafile/manzoor.256.757504713
channel ORA_DISK_1: reading from backup piece /u01/app/oracle/flash_recovery_area/TEST11G/backupset/2011_07_26/o1_mf_nnndf_TAG20110726T102018_72wkwk70_.bkp
channel ORA_DISK_1: piece handle=/u01/app/oracle/flash_recovery_area/TEST11G/backupset/2011_07_26/o1_mf_nnndf_TAG20110726T102018_72wkwk70_.bkp tag=TAG20110726T102018
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
Finished restore at 26-JUL-11

RMAN> recover tablespace manzoor;

Starting recover at 26-JUL-11
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4

starting media recovery
media recovery complete, elapsed time: 00:00:00

Finished recover at 26-JUL-11

RMAN> sql 'alter tablespace manzoor online';

sql statement: alter tablespace manzoor online

RMAN> exit

Wednesday, July 20, 2011

Info on Killing Session / Process

This is an answer given by Tom on Killing session:-


Hi Tom

When we do alter system kill 'sid, serial#'. Are we killing the user process or the
server process? Because sometimes i see that after the session is killed the query is still running in the sessions even shows KILLED sometimes takes days to disappear! Sometimes we got some problems with large batch jobs in testing enviroment, we killed from sql plus but the job keeps on going!
What happens if we kill the server process with kill -9 instead of issueing alter system
kill?


cheers
cheers


Toms Answer:-

The job isn't "going on" it is actually "going back" -- it is rolling back.

If the job took an hour to get where it is, it might take even LONGER to "undo" what its done.

You can monitor how the killed session is doing as far as rolling back is concerned in v$transaction via the used_ublk column.


Consider this example. In a schema "big_table" i started a delete against a 1,000,000 row indexed table. Partway through -- I killed it. This is what you'll see (you need to be more selective on the query against v$transaction of course, I just had one active DML session going)

ops$tkyte@ORA817DEV.US.ORACLE.COM> select used_ublk from v$transaction;

USED_UBLK
----------
5750

1 row selected.

it generated that much undo so far...

ops$tkyte@ORA817DEV.US.ORACLE.COM> @showsql

USERNAME SID_SERIAL STATUS MODULE ACTION
--------------- --------------- ---------- --------------- ---------------
OPS$TKYTE '7,665' ACTIVE 01@ showsql.sql

BIG_TABLE '8,1082' ACTIVE SQL*Plus


2 rows selected.

BIG_TABLE(8,1082) ospid = 13903 command = 7 program = sqlplus@aria-dev (TNS V1-V3)
dedicated server=13904
Monday 10:24 Monday 10:38 last et = 822
delete from big_table

I kill it and get the message "it is marked for kill". That means the server heard us and is in the process of rolling back but it'll take a while

ops$tkyte@ORA817DEV.US.ORACLE.COM> alter system kill session '8,1082';
alter system kill session '8,1082'
*
ERROR at line 1:
ORA-00031: session marked for kill

ops$tkyte@ORA817DEV.US.ORACLE.COM> select username, status from v$session where username = 'BIG_TABLE';

USERNAME STATUS
-------------------- ----------
BIG_TABLE KILLED

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> select used_ublk from v$transaction;

USED_UBLK
----------
5327

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> /

USED_UBLK
----------
5202

1 row selected.

we can watch the used_ublk shrink and even use that to estimate when it'll be done

ops$tkyte@ORA817DEV.US.ORACLE.COM> /

USED_UBLK
----------
4470

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> /

USED_UBLK
----------
2914

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> select username, status from v$session where username
= 'BIG_TABLE';

USERNAME STATUS
-------------------- ----------
BIG_TABLE KILLED

1 row selected.

session is still there...

ops$tkyte@ORA817DEV.US.ORACLE.COM> select used_ublk from v$transaction;

USED_UBLK
----------
2262

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> /

USED_UBLK
----------
1430

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> /

USED_UBLK
----------
1103

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> select username, status from v$session where username
= 'BIG_TABLE';

USERNAME STATUS
-------------------- ----------
BIG_TABLE KILLED

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> select used_ublk from v$transaction;

USED_UBLK
----------
1048

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> /

USED_UBLK
----------
489

1 row selected.

ops$tkyte@ORA817DEV.US.ORACLE.COM> /

no rows selected

now we are done rolling back and...

ops$tkyte@ORA817DEV.US.ORACLE.COM> select username, status from v$session where username
= 'BIG_TABLE';

no rows selected

ops$tkyte@ORA817DEV.US.ORACLE.COM>

the session is gone


If you kill -9, it might appear that the session is gone "faster" but not really. We still have to roll back (obviously). We might be doing on demand recovery of the blocks which makes it "appear" faster but we still have to recover the transaction. kill -9 is not something I recommend, especially if you use the above technique to see it rolling back.

Monday, July 4, 2011

Procedures Using VBA Example..

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset

Private Sub CommandButton1_Click()

Dim com As ADODB.Command

'without Parameter
'-----------------------------------------------------
con.ConnectionString = "Provider=MSDAORA;Data Source=orcl_man;User Id=manzoor;Password=ahamed;Persist Security Info = True;"
con.Open
Set com = New ADODB.Command
com.CommandText = "begin ins_emp; end;"
com.ActiveConnection = con
com.Execute
con.Close
'--------------------------------------
'--------------------------------------
'With Parameter
'---------------------------------------
Dim cmd As New ADODB.Command
Dim p1 As New ADODB.Parameter
Dim p2 As New ADODB.Parameter
Dim p3 As New ADODB.Parameter
Dim p4 As New ADODB.Parameter

con.ConnectionString = "Provider=MSDAORA;Data Source=orcl_man;User Id=manzoor;Password=ahamed;Persist Security Info = True;"
con.Open
i = 10
Do While i > 0

Set cmd = New ADODB.Command
cmd.CommandText = "begin ins_emp_with_para(?,?,?,?); end;"
cmd.ActiveConnection = con
Set p1 = cmd.CreateParameter("P1", adInteger, adParamInput, 8, i)
Set p2 = cmd.CreateParameter("p2", adLongVarChar, adParamInput, 30, "Ahamed")
Set p3 = cmd.CreateParameter("p3", adInteger, adParamInput, 8, 3)
Set p4 = cmd.CreateParameter("p4", adLongVarChar, adParamInput, 30, "Software")
cmd.Parameters.Append p1
cmd.Parameters.Append p2
cmd.Parameters.Append p3
cmd.Parameters.Append p4
cmd.Execute
i = i - 1
Loop

con.Close
MsgBox "Data been successfully Uploaded", vbInformation + vbOKOnly, "Health Check"

End Sub




To Commit or Rollback transaction:-

Syntax:-

Connection.BeginTrans
Connection.Execute(Insert sql statement)
Connection.Execute(Insert sql statement)
Connection.Execute(Insert sql statement)
Connection.Execute(Insert sql statement)
Connection.Execute(Insert sql statement)
Connection.CommitTrans
On Error
Connection.RollbackTrans

-----------------------------------------

Send mail using VBA

Sub sendmessagetouser()
Const Outlookmailitem As Integer = 0
Set OutlookApp = CreateObject("Outlook.Application")
Set outlookmail = OutlookApp.createitem(Outlookmailitem)
outlookmail.Display
outlookMail.From = "manzoor.ahamed@test.com"
outlookmail.To = "manzoor.ahamed@test.com"
outlookmail.Subject = "Test Message"
outlookmail.Body = "Hello " & Application.UserName & " This is a test"
outlookmail.attachments.Add "E:\testfile.txt"
outlookmail.Send
End Sub

Examples for using files

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder("E:\HKA_REPORT_TEMP")
k = f.Files.Count 'Get file count in a folder

-- Read from one file and write it to the other

Set ifs = fs.opentextfile("E:\input.sql")
Set a = fs.CreateTextFile("E:\output.sql", True)
a.WriteLine "This is test:
Do Until ifs.atendofstream
tt = ifs.readline
a.WriteLine tt
Loop
a.Close
ifs.Close

-- Copy files from one location to other
Set f = fs.GetFolder("E:\FILEFOLER")
Set fc = f.Files
For Each f1 In fc
s = f1.Name
FileCopy f1, "E:\APPLN\" & s
f1.Delete
Next

----Automate using vb script---
Dim args, objExcel
Set args = WScript.Arguments
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open "E:\HKA_REPORT\TABLESPACE_REPORT.xlsm"
objExcel.Visible = True
objExcel.Run "extractreport"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Quit
-------------------------------------------------------

Script to change the colour of the cell which contains data.
Sub TestForDups()

Dim LLoop As Integer
Dim LTestLoop As Integer
Dim LClearRange As String
Dim Lrows As Integer
Dim LRange As String
Dim LChangedValue As String
Dim LTestValue As String

'Test first 200 rows in spreadsheet for uniqueness
Lrows = 200
LLoop = 2

'Clear all flags
LClearRange = "A2:A" & Lrows
Range(LClearRange).Interior.ColorIndex = xlNone
'Check first 200 rows in spreadsheet
While LLoop <= Lrows
LChangedValue = "A" & CStr(LLoop)
If Len(Range(LChangedValue).Value) > 0 Then
'Test each value for uniqueness
LTestLoop = 2
While LTestLoop <= Lrows
If LLoop <> LTestLoop Then
LTestValue = "A" & CStr(LTestLoop)
'Value has been duplicated in another cell
If Range(LChangedValue).Value = Range(LTestValue).Value Then
'Set the background color to red
Range(LChangedValue).Interior.ColorIndex = 3
Range(LTestValue).Interior.ColorIndex = 3
End If
End If
LTestLoop = LTestLoop + 1
Wend
End If
LLoop = LLoop + 1
Wend
End Sub
-------------------------

simple folder creation script:-

Sub test()
Dim fs, f, s
m = "e:\thisisa test folder"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.createfolder(m)
s = f.DateCreated
MsgBox s
End Sub

-------
Help on VBA Site
http://www.ozgrid.com/VBA

Sheel Script - Easy Reference.

Vi Commands

To insert new text - esc + i ( You have to press 'escape' key then 'i')
To save file - esc + : + w (Press 'escape' key then 'colon' and finally 'w')
To save file with file name (save as) - esc + : + w "filename"
To quit the vi editor - esc + : + q
To quit without saving - esc + : + q!
To save and quit vi editor - esc + : + wq


To search for specified word in forward direction

esc + /word (Press 'escape' key, type /word-to-find,
for e.g. to find word 'shri', type as /shri)
To continue with search - n

To search for specified word in backward direction

esc + ?word (Press 'escape' key, type word-to-find)

To copy the line where cursor is located - esc + yy
To paste the text just deleted or copied at the cursor - esc + p
To delete entire line where cursor is located - esc + dd
To delete word from cursor position - esc + dw


To Find all occurrence of given word and Replace then globally
without confirmation

esc + :$s/word-to-find/word-to-replace/g

For. e.g. :$s/mumbai/pune/g

Here word "mumbai" is replace with "pune"



To Find all occurrence of given word and Replace then globally with confirmation

esc + :$s/word-to-find/word-to-replace/cg

To run shell command like ls, cp or date etc within vi

esc + :!shell-command

For e.g. :!pwd



Arithmetic Operators:-

1) expr op1 operator op2 ( Space is required by number and operators)

Eg:
expr 6 + 4
expr 6 - 4
expr 6 / 2
expr 6 % 4
expr 6 \* 3 (Since * is a wild card character we need to add a back slash before * symbol)

No space should be given while assigning a value to a variable

$ z=`expr 6 + 4`
$ echo $z


Shell Built in Variables Meaning

$# - Number of command line arguments. Useful to test no. of command line args in shell script.
$* - All arguments to shell
$@ - Same as above
$- - Option supplied to shell
$$ - PID of shell
$! - PID of last started background process (started with &)

Eg:-
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"


Some Commands:-

clear - clears the screen.


echo command:-

echo [options] [string, variables...]


echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l


-e Enable interpretation of the following backslash escaped characters in the strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash

About quotes:-

" Double Quotes - Anything enclose in double quotes removed meaning of that characters (except \ and $).
' Single quotes - Enclosed in single quotes remains unchanged.
` Back quote - To execute command

$ echo "Today is `date`"
Today is Tue Jun 14 11:08:49 BST 2011
$ echo 'Today is `date`'
Today is `date`

To get the exit status $?

Eg"

$ expr 6 + 3
$ echo $?


Input command:-

read

eg:-

echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"


Execute more than 1 command in CLI

$date;who

Use Semicolon (;) to execute more than one commands simultaneously.


Commands

sort
tr
diff


Filters:-

head , tail, uniq


Conditional Statement:-

Syntax:-
if condition
then
stmt
elif
stmt
else
stmt
fi


eg

#/bin/sh
# Script to test rm command and exist status
#
if rm $1
then
echo "$1 file deleted"
fi


Test Command:-

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]


test or [ expr ] works with
1.Integer ( Number without decimal point)
2.File types
3.Character strings

Integer:-

-eq - Equal to
-ne - Not equal to
-lt - Less than
-le - Less than or equal to
-gt - Greater than
-ge - Greater than or equal to

Eg:-

if test 5 -lt 6 then
if [ 5 -lt 6 ]; then

Strings :-


string1 = string2 - String1 equal to string2
string1 != string2 - String1 not equal to string2
string1 - string1 is not null or not defined
-n string1 - string1 is not null does exists
-z string1 - string1 is null and does exists


File type and directories:-

-s file Non empty file
-f file Is File exist or normal file and not a directory
-d dir Is Directory exist and not a file
-w file Is writeable file
-r file Is read-only file
-x file Is file is executable


Logical operators:-


!expression - Logical NOT
expression1 -a expression2 - Logical AND
expression1 -o expression2 - Logical OR


Loop commands:-

for loop

Syntax:-
for {varibale} in { list..}
do
stmt1
stmtn
done
eg:-

for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done

While Loop

Syntax:-

while [ condition ]
do
stmt1
stmtn
done


Case Statement:-

Syntax :-

case $varibale-name in
pattern1) command
command;;
pattern2) command
command;;
patternn) command
command;;
*) command
command;;
esac


Eg:-


rental=$1
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, I can not gat a $rental for you";;
esac


Debug Shell scripts:-

Syntax
sh option script_name

Option can be

-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable,
followed by the command and its expanded arguments.


I/O Redirections and File descriptions:-
> - Redirects to

Eg:-

cat file1 > file2

The contents of file1 will be redirected to file2


< - Gets from

cat < file2


The contents of file2 will be dispalyed, means the cat commands get the contents of file2.


In Linux (And in C programming Language) your keyboard, screen etc are all treated as files.
Following are name of such files

Standard File File Descriptors number Use Example
stdin 0 as Standard input Keyboard
stdout 1 as Standard output Screen
stderr 2 as Standard error Screen


By default in Linux every program has three files associated with it,
(when we start our program these three files are automatically opened by your shell). The use of first two files
(i.e. stdin and stdout) , are already seen by us. The last file stderr (numbered as 2) is used by our program to print error on screen.
You can redirect the output from a file descriptor directly to file with following syntax

Syntax:

file-descriptor-number>filename

Eg:-
Examples: (Assemums the file bad_file_name111 does not exists)
$ rm bad_file_name111
rm: cannot remove `bad_file_name111': No such file or directory
Above command gives error as output, since you don't have file. Now if we try to redirect this error-output to file,
it can not be send (redirect) to file, try as follows:

$ rm bad_file_name111 > er
Still it prints output on stderr as rm: cannot remove `bad_file_name111': No such file or directory,
and if you see er file as $ cat er , this file is empty, since output is send to error device and you can not
redirect it to copy this error-output to your file 'er'. To overcome this problem you have to use following command:

$ rm bad_file_name111 2>er
Note that no space are allowed between 2 and >, The 2>er directs the standard error output to file. 2 number is
default number (file descriptors number) of stderr file.


Eg:-

$ cat > demoscr
if [ $# -ne 2 ]
then
echo "Error : Number are not supplied"
echo "Usage : $0 number1 number2"
exit 1
fi
ans=`expr $1 + $2`
echo "Sum is $ans"

Run it as follows:
$ chmod 755 demoscr
$ ./demoscr
Error : Number are not supplied
Usage : ./demoscr number1 number2
$ ./demoscr > er1
$ ./demoscr 5 7
Sum is 12

For first sample run , our script prints error message indicating that you have not given two number.

For second sample run, you have redirect output of script to file er1, since it's error we have to show it to user,
It means we have to print our error message on stderr not on stdout. To overcome this problem replace above echo statements as follows
echo "Error : Number are not supplied" 1>&2
echo "Usage : $0 number1 number2" 1>&2
Now if you run it as follows:

$ ./demoscr > er1
Error : Number are not supplied
Usage : ./demoscr number1 number2


It will print error message on stderr and not on stdout. The 1>&2 at the end of echo statement,
directs the standard output (stdout) to standard error (stderr) device.
Syntax:
from>&destination



Functions:-

Syntax:-

function-name ()
{
command 1
command 2
.
.
command n
return
}

To automate function you need to add the function in /etc/bashrc file.

If you want to add particular function to particular user then open .bashrc file in users home directory as follows:

# vi .bashrc
OR
# mcedit .bashrc
At the end of file add following in .bashrc file
SayBye()
{
echo "Bye $LOGNAME ! Life never be the same, until you login again!"
echo "Press a key to logout. . ."
read
return
}

Tip: If you want to show some message or want to perform some action when you logout, Open file .bash_logout in your home
directory and add your stuff here For e.g. When ever I logout, I want to show message Buy! Then open your .bash_logout file
using text editor such as vi and add statement:
echo "Buy $LOGNAME, Press a key. . ."
read
return


getopts Command


This command is used to check valid command line argument are passed to script. Usually used in while loop.

Syntax:
getopts {optsring} {variable1}


getopts is used by shell to parse command line argument.
As defined in man pages:
"optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to
have an argument, which should be separated from it by white space. Each time it is invoked, getopts places the next
option in the shell variable variable1, When an option requires an argument, getopts places that argument into the
variable OPTARG. On errors getopts diagnostic messages are printed when illegal options or missing option
arguments are encountered. If an illegal option is seen, getopts places ? into variable1."

Example:-
We have script called ani which has syntax as
ani -n -a -s -w -d
Options: These are optional argument
-n name of animal
-a age of animal
-s sex of animal
-w weight of animal
-d demo values (if any of the above options are used their values are not taken)

Above ani script is as follows:
-------------------------------------------------------------------------------------
$ vi ani
#
# Usage: ani -n -a -s -w -d
#
#
# help_ani() To print help
#
help_ani()
{
echo "Usage: $0 -n -a -s -w -d"
echo "Options: These are optional argument"
echo " -n name of animal"
echo " -a age of animal"
echo " -s sex of animal "
echo " -w weight of animal"
echo " -d demo values (if any of the above options are used "
echo " their values are not taken)"
exit 1
}
#
#Start main procedure
#
#
#Set default value for variable
#
isdef=0
na=Moti
age="2 Months" # may be 60 days, as U like it!
sex=Male
weight=3Kg
#
#if no argument
#
if [ $# -lt 1 ]; then
help_ani
fi
while getopts n:a:s:w:d opt
do
case "$opt" in
n) na="$OPTARG";;
a) age="$OPTARG";;
s) sex="$OPTARG";;
w) weight="$OPTARG";;
d) isdef=1;;
\?) help_ani;;
esac
done
if [ $isdef -eq 0 ]
then
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)"
else
na="Pluto Dog"
age=3
sex=Male
weight=20kg
echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"
fi
-------------------------------------------------------------------------------------
Save it and run as follows to view the output.
$ chmod +x ani
$ ani -n Lassie -a 4 -s Female -w 20Kg
$ ani -a 4 -s Female -n Lassie -w 20Kg
$ ani -n Lassie -s Female -w 20Kg -a 4


Essential Utilities:-


cut utility

Syntax:
cut -f{field number} {file-name}


Paste utility
Syntax:
paste {file1} {file2}

tr (translate utility)

Syntax:
tr {pattern-1} {pattern-2}


eg:-
tr "[a-z]" "[A-Z]" < file_name

awk utility:-

General Syntax of awk utility:
Syntax:
awk 'pattern action' {file-name}

filename -> inventory
egg order 4
cacke good 10
cheese okay 4
pen good 12
floppy good 5

After crating file issue command
$ awk '/good/ { print $3 }' inventory
10
12
5




sed utility - Editing file without using editor (sed - Steam line editor )
For this part of tutorial create data file as follows

Syntax:
sed {expression} {file}

Use of sed utility: sed is used to edit (text transformation) on given stream i.e a file or may be input from a pipeline.

check below example
filename : teaormilk

India's milk is good.
tea Red-Lable is good.
tea is better than the coffee.

After creating file give command
$ sed '/tea/s//milk/g' teaormilk > /tmp/result.tmp.$$
$ cat /tmp/result.tmp.$$:
India's milk is good.
milk Red-Lable is good.
milk is better than the coffee


Removing Duplicates:-

$ uniq filename

Expression:-

:p - Print line
:1,5 p - Print first 5 lines
:2 p - Print 2nd line.
:1,$ p - Print the whole file
:set number - It will set numbers to the lines.
:set nonubmer - Removes the numbers.
:1,d - delete first line
:1,5 d - delete first 5 lines.
:1,4 co $ - copy lines 1-4 and paste at the the end of the file. ($ end of the file)
:/linux/p - Find the word linux and print it.
:g /linux/p - Find the word linux on the entrie file and print it.
:1,$ /linux/p - Find the word linux on the entrie file and print it.
:8 s/learns/learn - Find the word learns in line 8 and replace it with learn
:1,$ s/learns/learn - Find the word learns in the entrie file and replace it with learn.
:g /sister/ s/never/always/ - Find the word sister and in that line if there is never then it will get replaced with always.
:g /Unix/ s/Unix/linux/ - Find the word unix and replace it with linux
:g /Unix/ s//linux/ - Find the word unix and replace it with linux (short cut)
:g /brother/s/also/xyz/ - Find the first occuerence of the word also and it will be replaced with xyz.
:g /brother/s/also/xys/g - Find all the occurence of the word also and it will be replaced with xyz.
:g /linux/s//unix/gc - Replace a word with users confirmation.
:g /the/p - Will find word like theater, the, brother, other etc.
:g /\ - Exact word.
:/^$ - Find empty line
:g /^$ - All blanks lines.
:g/[^/^$] - view file without blank lines.
:g /^$/d - Delete all the blank lines in a file
:u - Undo the last action.
:g/linux/p - Prints only linux
:g/[Ll]inux/p - Print linux and Linux
:g/[0-9] - number ranges.

Predenfied ranges:-

[:alnum:] - Letters and Digits (A to Z or a to z or 0 to 9)
[:alpha:] - Letters A to Z or a to z
[:cntrl:] - Delete character or ordinary control character (0x7F or 0x00 to 0x1F)
[:digit:] - Digit (0 to 9)
[:graph:] - Printing character, like print, except that a space character is excluded
[:lower:] - Lowercase letter (a to z)
[:print:] - Printing character (0x20 to 0x7E)
[:punct:] - Punctuation character (ctrl or space)
[:space:] - Space, tab, carriage return, new line, vertical tab, or form feed (0x09 to 0x0D, 0x20)
[:upper:] - Uppercase letter (A to Z)
[:xdigit:] - Hexadecimal digit (0 to 9, A to F, a to f)

eg: instead of writing :/[0-9A-Za-Z] we can write as :/[[:alnum:]]/

:g/c.*and

. (DOT) is special command of linux.

Here first c character is matched, then any single character (.) followed by n number of single character
(1 or 100 times even) and finally ends with and. This can found different word as follows command or catand etc

:1,$ s/[a-z]/\u &/g - Convert lower case to upper case.

1,$ Line Address location is all i.e. find all lines for following pattern
s Substitute command
/[a-z]/ Find all lowercase letter - Target
\u&/ Substitute to Uppercase. \u& means substitute last patter (&) matched with its UPPERCASE replacement (\u)
Note: Use \l (small L) for lowercase character.
g Global replacement



awk Examples:-

filename :- inven

Sr.No Product Qty Unit Price
1 Pen 5 20.00
2 Rubber 10 2.00
3 Pencil 3 3.50
4 Cock 2 45.50

$awk '{ print $1 $2 "--> Rs." $3 * $4 }' inven
$awk '{ print $2 $4}' inven
$awk '{ print $0 }' inven


You can easily, do the arithmetic with awk as follows


$ cat > math
{
print $1 " + " $2 " = " $1 + $2
print $1 " - " $2 " = " $1 - $2
print $1 " / " $2 " = " $1 / $2
print $1 " x " $2 " = " $1 * $2
print $1 " mod " $2 " = " $1 % $2
}

$ awk -f math
20 3
20 + 3 = 23
20 - 3 = 17
20 / 3 = 6.66667
20 x 3 = 60
20 mod 3 = 2
(Press CTRL + D to terminate)
------------------------------

Hi Use the \ operator in sheel scripts if you are using V$ tables.
Eg:-

V\$sql.













Thanks to Vivek G. Gite