awk to loop lines and print substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to loop lines and print substring
# 1  
Old 08-08-2012
awk to loop lines and print substring

The input data:
Code:
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:
Code:
        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:
Code:
SAP_J2EEClusterNode=3620851 deployment_forbidden=off

Desired output:
Code:
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.
# 2  
Old 08-08-2012
END block will execute only once.

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

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

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 08-08-2012
Hi


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


Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 4  
Old 08-08-2012
Thanks.. removed the END block but got this output:
Code:
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:
Code:
SAP_J2EEClusterNode=3620850
 deployment_forbidden=off
SAP_J2EEClusterNode=3620851
 deployment_forbidden=off

# 5  
Old 08-08-2012
try this
Code:
awk -F, '/mbeanName/{printf("%s",$3)}/Get attribute Properties/{printf(" %s\n",$2)}' file


Last edited by Franklin52; 08-08-2012 at 08:40 AM.. Reason: code tags
This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 08-08-2012
thanks.. works perfect now!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

Looking for an awk command to print strings only if substring is missing

I have a file that I need to find each interface that has move-group on the interface line and print this line if the lines under the interface does Not have "filter-shared 14". Example file: interface 1/1/1/0 move-group decription one one one zero no shut filter-shared 14... (21 Replies)
Discussion started by: numele
21 Replies

5. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

6. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

7. Shell Programming and Scripting

For loop and awk print

Hi, i am using a variable tmpVar, using variable data i need implement for loop tmpVar="abc bbc cbc nbc mbc" # valiable having total number of words= 5 so i need to loop 5 times. tmpVar="abc bbc cbc nbc mbc" tmpcnt=`echo $tmpVar|wc -w` for cnt in 1..$tmpcnt do t1=`echo... (4 Replies)
Discussion started by: onesuri
4 Replies

8. Shell Programming and Scripting

AWK and print next lines #3 thru #10

I have a output log file, that I want to extract some temperature measurement data. I want to AWK on the words "show chassis environment" in the original file, and extract that entire line, and then the 3rd to 10th lines after the one I AWK'd, into a seperate output file. Here is an example... (3 Replies)
Discussion started by: HikerLT
3 Replies

9. Shell Programming and Scripting

loop + sum + print using awk

Hi, I am unable sum of each column in the loop usng awk command. Awk is not allowing the parameters in the command. i am facing the below error. awk: 0602-562 Field $() is not correct. Source file abc.txt 100,200,300,400,500,600,700,800,900 101,201,301,401,501,601,701,801,901 ... (1 Reply)
Discussion started by: number10
1 Replies
Login or Register to Ask a Question