Automic Workload Automation

Expand all | Collapse all

Splitting strings that contain a space separator of different lengths

Jump to Best Answer
  • 1.  Splitting strings that contain a space separator of different lengths

    Posted 06-26-2018 07:35 PM

    Hi,

     

    Using the Automic Scripting. I'm trying to split the string below 

     

    "MplOpZipService                          Started         20180627 06:06:08.630  N/A"

     

    into an array

     

    ["MplOpZipService ","Started","20180627","06:06:08.630","N/A"]

     

    I have no issues doing this using STR_SPLIT (:FILL &STRINGS#[] = STR_SPLIT(&STRING#," ")) if the amount of blank spaces between elements was always constant.

    Because the separator could be one or many blank spaces, I can't think of how to do this? 

     

    If any one could point me in the right direction that would be fantastic.

     

    Cheers

    Ben 



  • 2.  Re: Splitting strings that contain a space separator of different lengths

    Posted 06-26-2018 09:10 PM

    Hi BenSumner612127 ,

     

    Can you incorporate a STR_TRIM afterwards?

     

    Cheers,

    Alexander



  • 3.  Re: Splitting strings that contain a space separator of different lengths

    Posted 06-27-2018 06:23 AM

    Hi Ben,

     

     if you really need to have the split logic in the Automation Engine, then this could solve your problem:

     

    :SET &STRING# = "MplOpZipService                          Started         20180627 06:06:08.630  N/A"
    !
    ! Service (MplOpZipService)
    :SET &SERVICE_HIT# = STR_FIND(&STRING#," ")
    :SET &HELP_STRING# = STR_CUT(&STRING#,&SERVICE_HIT#)
    :SET &SERVICE# = STR_SUB(&STRING#,&HELP_STRING#,"")
    !
    ! NA (N/A)
    :SET &NA_HIT# = STR_FIND_REV(&STRING#," ")
    :SET &NA# = STR_CUT(&STRING#,&NA_HIT#)
    :SET &NA# = STR_TRIM(&NA#)
    !
    ! Status (Started)
    :SET &HELP_STRING2# = STR_SUB(&HELP_STRING#,&NA#,"")
    :SET &HELP_STRING2# = STR_TRIM(&HELP_STRING2#)
    :SET &STATUS_HIT# = STR_FIND(&HELP_STRING2#," ")
    :SET &HELP_STRING3# = STR_CUT(&HELP_STRING2#,&STATUS_HIT#)
    :SET &STATUS# = STR_SUB(&HELP_STRING2#,&HELP_STRING3#)
    :SET &STATUS# = STR_TRIM(&STATUS#)
    !
    !Date (20180627)
    :SET &HELP_STRING4# = STR_TRIM(&HELP_STRING3#)
    :SET &DATE_HIT# = STR_FIND(&HELP_STRING4#," ")
    :SET &DATE# = STR_CUT(&HELP_STRING4#,1,&DATE_HIT#)
    :SET &DATE# = STR_TRIM(&DATE#)
    !
    !Time (06:06:08.630)
    :SET &TIME# = STR_SUB(&HELP_STRING4#,&DATE#,"")
    :SET &TIME# = STR_TRIM(&TIME#)
    !
    !Fill Array
    :DEFINE &STRINGS#, string,5
    :SET &ARRAYSTRING# = "&SERVICE#,&STATUS#,&DATE#,&TIME#,&NA#"
    :FILL &STRINGS#[] = STR_SPLIT(&ARRAYSTRING#,",")
    :PRINT &STRINGS#[1],&STRINGS#[2],&STRINGS#[3],&STRINGS#[4],&STRINGS#[5]

     

    But I would suggest you do the splitting already on OS side and just populate those variables in the Automation Engine via :REGISTER_VARIABLE, then the AE script could look like this :

     

    !REGISTER_VARIABLE <AE variable>, windows or linux variable

    :REGISTER_VARIABLE SERVICE#,  %SERVICE% or $SERVICE
    :REGISTER_VARIABLE NA#,  %NA% or  $NA
    :REGISTER_VARIABLE STATUS#,  %STATUS% or  $STATUS
    :REGISTER_VARIABLE DATE#,  %DATE% or  $DATE
    :REGISTER_VARIABLE TIME#,  %TIME% or  $TIME
    !
    !Fill Array
    :DEFINE &STRINGS#, string,5
    :SET &ARRAYSTRING# = "&SERVICE#,&STATUS#,&DATE#,&TIME#,&NA#"
    :FILL &STRINGS#[] = STR_SPLIT(&ARRAYSTRING#,",")
    :PRINT &STRINGS#[1],&STRINGS#[2],&STRINGS#[3],&STRINGS#[4],&STRINGS#[5]



  • 4.  Re: Splitting strings that contain a space separator of different lengths
    Best Answer

    Posted 06-27-2018 01:56 PM

    Here is a small piece of code to remove all duplicate spaces in a string;

     

    :set &teststring# = "a    b c   d"
    :print "start: &teststring#"
    :while str_find(&teststring#,"  ") > 0
    : set &teststring# = str_sub(&teststring#,"  "," ")
    :endwhile
    :print "end : &teststring#"



  • 5.  Re: Splitting strings that contain a space separator of different lengths

    Posted 06-27-2018 06:11 PM

    Thanks Pete…

     

    After a little tweak, it worked perfectly!!