Archive: Extract & parse command line output


Extract & parse command line output
What is the best method for parsing the output from a commandline tool? If this was C#, I'd probably just use a regular expression...

Sample commandline output:


CLUSTER_NAME STATUS MASTER_HOST ADMIN HOSTS SERVERS
trg7u4 ok symph9 abcadmin 23 21
trg7demo ok foo abcadmin 12 11

I need to extract "trg7u4" and "abcadmin" from that spew. I'd use something like nsExec::ExecToStack to run the program. I've ran across the NSISpcre_plug-in for REGEX. Also found arrays plugin which could be another option.

Any other better/efficient methods to get the data I need?

No recommended methods for parsing commandline output eh?


This can be done using only StrCpy, StrLen and StrCmp. Also, I think ExecToLog is what you're looking for, not ExecToStack.


Originally posted by MSG
This can be done using only StrCpy, StrLen and StrCmp.
Hows that? The spacing and names of those fields change depending on the users setup.

I would start with searching for two spaces in a row. Then skip all spaces, and voila I'm at the beginning of the next column. Do this for x columns and you're at the ADMIN column. Similarly, reading the entry would simply be a matter of reading all data until the first double space. (Or the first space, if the data cannot contain spaces.)


Ended up using a ghetto solution in conjunction with StrTok


nsExec::ExecToStack /TIMEOUT=${EXECTIMEOUT} '"command"'
Pop $0 # return value/error/timeout
Pop $1 # printed text, up to ${NSIS_MAX_STRLEN}
; Need better way to skip to the 2nd line of the LSCLUSTERS command output...

;write COMMAND output to file to easily read/parse
FileOpen $0 $PLUGINSDIR\command.log w
FileWrite $0 "$1"
FileClose $0

;re-open file we just wrote. skip to the 2nd line
FileOpen $0 $PLUGINSDIR\command.log r
FileRead $0 $1 ; headers
FileRead $0 $2 ; first row, which should be the localhost details
FileClose $0
MessageBox MB_OK "command_2: $2"
${StrTok} $5 "$2" " " "0" "1" ;cluster name
${StrTok} $6 "$2" " " "2" "1" ;cluster master
${StrTok} $7 "$2" " " "3" "1" ;cluster admin
MessageBox MB_OK "tok0: $5. tok2: $6. tok3: $7."

StrLen $R1 $5 ;cluster name
${If} $R1 > 0
StrCpy $Cluster_Name $5
${EndIf}

StrLen $R1 $6 ;cluster master
${If} $R1 > 0
StrCpy $Cluster_Master $6
${EndIf}

StrLen $R1 $7 ;cluster admin
${If} $R1 > 0
StrCpy $Cluster_User $7
!insertmacro INSTALLOPTIONS_WRITE "cluster_account.ini" "Field 1" "State" ".\$Cluster_User"
${EndIf}