Skip to main content
The -F takes ERE even with POSIX
Source Link
Chris Davies
  • 128.3k
  • 16
  • 179
  • 324

This awk command will take the output from your srvctl command - or at least one that's formatted similarly to the one posted in the question - and generate a command you can use to repeat the configuation:

srvctl config database -d MYDB | awk -F':' *' ' /^Database unique name/ { dun=$2; gsub("^ *", "", dun)dun=$2 } /^Oracle home/ { oh=$2; gsub("^ *", "", oh)oh=$2 } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' 

Output:

srvctl add database -d MYDB -o /path/to/home 

Or, to match your requirement more closely:

srvctl config database -d MYDB >srvctl.txt awk -F':' *' ' /^Database unique name/ { dun=$2; gsub("^ *", "", dun)dun=$2 } /^Oracle home/ { oh=$2; gsub("^ *", "", oh)oh=$2 } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' srvctl.txt 

Output:

srvctl add database -d MYDB -o /path/to/home 

You can, of course, crash the multiple lines of each awk offering into a single line if you must, but it's far less readable:

 awk -F': *' '/^Database/{dun=$2} /^Oracle home/{oh=$2} END{printf "srvctl add database -d %s -o %s\n",dun,oh}' 

This awk command will take the output from your srvctl command - or at least one that's formatted similarly to the one posted in the question - and generate a command you can use to repeat the configuation:

srvctl config database -d MYDB | awk -F':' ' /^Database unique name/ { dun=$2; gsub("^ *", "", dun) } /^Oracle home/ { oh=$2; gsub("^ *", "", oh) } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' 

Output:

srvctl add database -d MYDB -o /path/to/home 

Or, to match your requirement more closely:

srvctl config database -d MYDB >srvctl.txt awk -F':' ' /^Database unique name/ { dun=$2; gsub("^ *", "", dun) } /^Oracle home/ { oh=$2; gsub("^ *", "", oh) } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' srvctl.txt 

Output:

srvctl add database -d MYDB -o /path/to/home 

This awk command will take the output from your srvctl command - or at least one that's formatted similarly to the one posted in the question - and generate a command you can use to repeat the configuation:

srvctl config database -d MYDB | awk -F': *' ' /^Database/ { dun=$2 } /^Oracle home/ { oh=$2 } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' 

Output:

srvctl add database -d MYDB -o /path/to/home 

Or, to match your requirement more closely:

srvctl config database -d MYDB >srvctl.txt awk -F': *' ' /^Database/ { dun=$2 } /^Oracle home/ { oh=$2 } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' srvctl.txt 

Output:

srvctl add database -d MYDB -o /path/to/home 

You can, of course, crash the multiple lines of each awk offering into a single line if you must, but it's far less readable:

 awk -F': *' '/^Database/{dun=$2} /^Oracle home/{oh=$2} END{printf "srvctl add database -d %s -o %s\n",dun,oh}' 
Source Link
Chris Davies
  • 128.3k
  • 16
  • 179
  • 324

This awk command will take the output from your srvctl command - or at least one that's formatted similarly to the one posted in the question - and generate a command you can use to repeat the configuation:

srvctl config database -d MYDB | awk -F':' ' /^Database unique name/ { dun=$2; gsub("^ *", "", dun) } /^Oracle home/ { oh=$2; gsub("^ *", "", oh) } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' 

Output:

srvctl add database -d MYDB -o /path/to/home 

Or, to match your requirement more closely:

srvctl config database -d MYDB >srvctl.txt awk -F':' ' /^Database unique name/ { dun=$2; gsub("^ *", "", dun) } /^Oracle home/ { oh=$2; gsub("^ *", "", oh) } END { printf "srvctl add database -d %s -o %s\n", dun, oh } ' srvctl.txt 

Output:

srvctl add database -d MYDB -o /path/to/home