awk to loop lines and print substring

The input data:

mbeanName: com.sap.default:name=tc~bl~deploy_controller,j2eeType=SAP_J2EEServicePerNode,SAP_J2EEClusterNode=3620850,SAP_J2EECluster=XXX
Get attribute Properties: {undepl_parallelism_strategy=normal, deployment_forbidden=off, locking_retries=50, suppress_ds_warnings=on, locking_interval=2000, os_pass=<OS_Pass>, archives_dir_name=archives, offline_result_timeout=3600000, undepl_offline_strategy=normal, depl_parallelism_strategy=normal, wed_disp_server_info=\\<HOST>/saploc/<SID>/SYS/profile/<CLUSTER_INFO>, os_user=<OS_User>, inst_id_2_inst_pfl=<INSTANCE_ID>=\\\\<HOST>/saploc/<SID>/SYS/profile/<PROFILE>
<INSTANCE_ID>=\\\\<HOST>/saploc/<SID>/SYS/profile/<PROFILE>, suppress_fatal_ds_warnings=off, min_free_bytes_to_deploy=104857600, storage_dir_name=storage, depl_offline_strategy=normal, deploy_threads=3}

mbeanName: com.sap.default:name=tc~bl~deploy_controller,j2eeType=SAP_J2EEServicePerNode,SAP_J2EEClusterNode=3620851,SAP_J2EECluster=XXX
Get attribute Properties: {undepl_parallelism_strategy=normal, deployment_forbidden=off, locking_retries=50, suppress_ds_warnings=on, locking_interval=2000, os_pass=<OS_Pass>, archives_dir_name=archives, offline_result_timeout=3600000, undepl_offline_strategy=normal, depl_parallelism_strategy=normal, wed_disp_server_info=\\<HOST>/saploc/<SID>/SYS/profile/<CLUSTER_INFO>, os_user=<OS_User>, inst_id_2_inst_pfl=<INSTANCE_ID>=\\\\<HOST>/saploc/<SID>/SYS/profile/<PROFILE>
<INSTANCE_ID>=\\\\<HOST>/saploc/<SID>/SYS/profile/<PROFILE>, suppress_fatal_ds_warnings=off, min_free_bytes_to_deploy=104857600, storage_dir_name=storage, depl_offline_strategy=normal, deploy_threads=3}

The awk statement:

        awk 'BEGIN {
                    FS = ","
                   }
                   {
                    if ($1 ~ /mbeanName:/) {
                        substring1 = $3
                    } else if ($1 ~ /Get attribute Properties:/) {
                        substring2 = $2
                    }
                   }
        END {print substring1 substring2}'

But the output only shows:

SAP_J2EEClusterNode=3620851 deployment_forbidden=off

Desired output:

SAP_J2EEClusterNode=3620850 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off

I know I need to put the substrings in an array so that I can print all the occurrences of substring1 and substring2, but I am not familiar with the awk syntax.

Please advise if you happen to stumble on this thread. Thanks in advance.

END block will execute only once.

So, your substring1, substring2 will be overwritten and only the last value will remain in those variable.

 
awk 'BEGIN {
                    FS = ","
                   }
                   {
                    if ($1 ~ /mbeanName:/) {
                        substring1 = $3
                    } else if ($1 ~ /Get attribute Properties:/) {
                        substring2 = $2
                    }
                       print substring1 substring2
                   }'
1 Like

Hi

$ awk -F, '/mbeanName/{printf $3}/Get attribute Properties/{print $2}' file
SAP_J2EEClusterNode=3620850 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off

Guru.

1 Like

Thanks.. removed the END block but got this output:

SAP_J2EEClusterNode=3620850
SAP_J2EEClusterNode=3620850 deployment_forbidden=off
SAP_J2EEClusterNode=3620850 deployment_forbidden=off
SAP_J2EEClusterNode=3620850 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off
SAP_J2EEClusterNode=3620851 deployment_forbidden=off

And tried the one-liner proposed by Guru and we have:

SAP_J2EEClusterNode=3620850
 deployment_forbidden=off
SAP_J2EEClusterNode=3620851
 deployment_forbidden=off

try this

awk -F, '/mbeanName/{printf("%s",$3)}/Get attribute Properties/{printf(" %s\n",$2)}' file
1 Like

thanks.. works perfect now!