Thursday, January 24, 2019

awk -F option

I often need to extract subset of data from one command so that it could be used in another during the DBA tasks performed through shell scripts.

One such requirement is that when you need to feed the name of ASM disks to the querydisks command.


For example, if you want to see the mapping of ASM disks to the devices at OS level, you need to run following command:

oracleasm querydisk -d

Now in order to get ASM diskname, you need to run

oracleasm listdisks

Now a sample output from oracleasm querydisk would be :

Disk "MYDISK" is a valid ASM disk on device [3,7]

In the above output, the 3 is the minor and 7 is major number of disk, which is often visible in OS level commands like iostat. How would you extract that 3 and 7 from above output?

Well, one way is to use awk -F command like following:

In order to extract 3, you would do:

Cat Disk "MYDISK" is a valid ASM disk on device [3,7] | awk -F[ '{print $2}' | awk -F, '{print $1}'

In order to extract 7, you would do:

Cat Disk "MYDISK" is a valid ASM disk on device [3,7] | awk -F[ '{print $2}' | awk -F, '{print $2}'

Hope that helps

2 comments:

Mikhail Velikikh said...

Hi Fahd,
I know that GNU awk supports regular expressions in the -F option, so that the device numbers in your example could be extracted like this:
echo 'Disk "MYDISK" is a valid ASM disk on device [3,7]' | awk -F'[\\[,\\]]' '{print $2"-"$3}'
3-7

Fahd Mirza said...

Thanks Mikhail.