Command output formatting

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Command output formatting

#1 Post by suadhm » 16 Aug 2023 01:00

Hi,
I have to produce command output on Windows machine on similar way that I can produce it on Linux machine.

Command is:

Code: Select all

crsctl stat res -t
And its output on Windows machine is:

Code: Select all

C:\Users\Administrator>crsctl stat res -t
--------------------------------------------------------------------------------
NAME           TARGET  STATE        SERVER                   STATE_DETAILS
--------------------------------------------------------------------------------
Local Resources
--------------------------------------------------------------------------------
ora.DATA.dg
               ONLINE  ONLINE       win2008
ora.FRA.dg
               ONLINE  ONLINE       win2008
ora.LISTENER.lsnr
               ONLINE  ONLINE       win2008
ora.asm
               ONLINE  ONLINE       win2008                  Started
ora.ons
               OFFLINE OFFLINE      win2008
--------------------------------------------------------------------------------
Cluster Resources
--------------------------------------------------------------------------------
ora.cssd
      1        ONLINE  ONLINE       win2008
ora.evmd
      1        ONLINE  ONLINE       win2008
ora.finnew.db
      1        OFFLINE OFFLINE
I can format it on Linux machine so it looks nicer with command:

Code: Select all

crsctl status res |grep -v "^$"|awk -F "=" 'BEGIN {print " "} {printf("%s",NR%4 ? $2"|" : $2"\n")}'|sed -e 's/  *, /,/g' -e 's/, /,/g'|\
awk -F "|" 'BEGIN { printf "%-40s%-35s%-20s%-50s\n","Resource Name","Resource Type","Target ","State" }{ split ($3,trg,",") split ($4,st,",")}{for (i in trg) {printf "%-40s%-35s%-20s%-50s\n",$1,$2,trg[i],st[i]}}'
And output looks like:

Code: Select all

Resource Name                           Resource Type                      Target              State
ora.DATA.dg                             ora.diskgroup.type                 ONLINE              ONLINE on oracle-linux-test-vm
ora.FRA.dg                              ora.diskgroup.type                 ONLINE              ONLINE on oracle-linux-test-vm
ora.LISTENER.lsnr                       ora.listener.type                  ONLINE              ONLINE on oracle-linux-test-vm
ora.asm                                 ora.asm.type                       ONLINE              ONLINE on oracle-linux-test-vm
ora.cssd                                ora.cssd.type                      ONLINE              ONLINE on oracle-linux-test-vm
ora.diskmon                             ora.diskmon.type                   OFFLINE             OFFLINE
ora.evmd                                ora.evm.type                       ONLINE              ONLINE on oracle-linux-test-vm
ora.ons                                 ora.ons.type                       OFFLINE             OFFLINE
ora.testdb.db                           ora.database.type                  ONLINE              ONLINE on oracle-linux-test-vm
Is there any way that this nicer formatting of command output can be done on Windows machine?

Regards.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Command output formatting

#2 Post by Aacini » 16 Aug 2023 15:10

I don't understand where "Resource Type" and the last part of "State" columns comes from...

Antonio

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Command output formatting

#3 Post by suadhm » 17 Aug 2023 04:36

Hi,

"Resource Type" is obtained combining "Local Resources" and "Cluster Resources" from original command, and "State" is obtained by combining "STATE" and "SERVER" from original command.

Output on Windows does not need to look same as on Linux. It need to be just simple to read.
May be it can look like:

Code: Select all

------------------------------------------------------------------------------------------
NAME               TARGET  STATE        SERVER     STATE_DETAILS    Local/Remote Resources
------------------------------------------------------------------------------------------
ora.DATA.dg	    ONLINE  ONLINE	 win2008      
ora.FRA.dg	    ONLINE  ONLINE       win2008
ora.LISTENER.lsnr   ONLINE  ONLINE       win2008
ora.asm		    ONLINE  ONLINE       win2008    Started
ora.ons	            OFFLINE OFFLINE      win2008

Regards,

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Command output formatting

#4 Post by Aacini » 17 Aug 2023 19:28

Aacini wrote:
16 Aug 2023 15:10
I don't understand where "Resource Type" and the last part of "State" columns comes from...

Antonio
suadhm wrote:
17 Aug 2023 04:36
Hi,

"Resource Type" is obtained combining "Local Resources" and "Cluster Resources" from original command, and "State" is obtained by combining "STATE" and "SERVER" from original command.
Mmm... No... The ora.diskgroup.type in "Resource Type" don't appears at any place in the input file, nor the on oracle-linux-test-vm in last part of "State"...

Code: Select all

@echo off
setlocal EnableDelayedExpansion

crsctl stat res -t > input.txt

set "spaces= "
for /L %%i in (1,1,4) do set "spaces=!spaces!!spaces!"
set /A "omit=0, n=0"
for /F "delims=" %%a in (input.txt) do (
   set /A n+=1
   if !n! leq 3 (
      if !n! equ 2 (
         set "line=%%a"
         echo !line:~0,15!     !line:~15!
         set "line="
      ) else (
         echo %%a
      )
   ) else if !n! gtr 5 (
      if !omit! gtr 0 (
         set /A omit-=1
      ) else (
         if not defined line (
            set "line=%%a%spaces%"
            if "!line:~0,1!" equ "-" set "omit=2" & set "line="
         ) else (
            set "line2=%%a"
            echo !line:~0,20!!line2:~15!
            set "line="
         )
      )
   )
)
Output:

Code: Select all

--------------------------------------------------------------------------------
NAME                TARGET  STATE        SERVER                   STATE_DETAILS
--------------------------------------------------------------------------------
ora.DATA.dg         ONLINE  ONLINE       win2008
ora.FRA.dg          ONLINE  ONLINE       win2008
ora.LISTENER.lsnr   ONLINE  ONLINE       win2008
ora.asm             ONLINE  ONLINE       win2008                  Started
ora.ons             OFFLINE OFFLINE      win2008
ora.cssd            ONLINE  ONLINE       win2008
ora.evmd            ONLINE  ONLINE       win2008
ora.finnew.db       OFFLINE OFFLINE
Antonio

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Command output formatting

#5 Post by suadhm » 18 Aug 2023 00:39

Hi Aacini,

That's great, it works as expected. Thank you very much!

All mentioned are columns from command output:

Code: Select all

C:\Users\Administrator>crsctl stat res -t
--------------------------------------------------------------------------------
NAME           TARGET  STATE        SERVER                   STATE_DETAILS
--------------------------------------------------------------------------------
Local Resources
--------------------------------------------------------------------------------
ora.DATA.dg
               ONLINE  ONLINE       win2008
ora.FRA.dg
               ONLINE  ONLINE       win2008
ora.LISTENER.lsnr
               ONLINE  ONLINE       win2008
ora.asm
               ONLINE  ONLINE       win2008                  Started
ora.ons
               OFFLINE OFFLINE      win2008
--------------------------------------------------------------------------------
Cluster Resources
--------------------------------------------------------------------------------
ora.cssd
      1        ONLINE  ONLINE       win2008
ora.evmd
      1        ONLINE  ONLINE       win2008
ora.finnew.db
      1        OFFLINE OFFLINE
We have all column here (STATE, SERVER, Local Resources and Cluster Resources) but output is not so readable.

Thank you! And best regards.

Post Reply