Format Data - awk ..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format Data - awk ..
# 1  
Old 11-25-2015
Format Data - awk ..

Code:
 
/clusters/cluster-1/exports/storage-views/M1_CRE03_SV:
Name                      Value
------------------------  ---------------------------------------------------------------------------------------------------
caw-enabled               true
controller-tag            -
initiators                [AM112_A, AM112_B, KEC12_C, ABL12_D, BL13_A,
                          BL13_B, ZBL13_C, ZBL13_D, BL14_A, ZBL14_B,
                          ZBL14_C, BL14_D]
operational-status        ok
 
/clusters/cluster-1/exports/storage-views/M1P5_T2_SV:
Name                      Value
------------------------  -----------------------------------------------------------------------------------------
caw-enabled               true
controller-tag            -
initiators                [G105_A, CIG105_B, CIG105_C, CIG105_D]
operational-status        ok

Code:
 
 Output Needed
 M1_CRE03_SV,AM112_A;AM112_B;KEC12_C;ABL12_D;BL13_A;BL13_B;ZBL13_C;ZBL13_D;BL14_A;ZBL14_B;ZBL14_C;BL14_D
 M1P5_T2_SV,G105_A;CIG105_B;CIG105_C;CIG105_D

I tried this ... but not getting the required output ...
Code:
 
 gawk 'BEGIN{ RS="\n" }
/storage-views\// { gsub( /\//," ",$0);view=$NF ;DD=""}
/^initiators/,/^operational-status/ { DD=DD"|"$1
print "%s\n",view","DD
}' input

# 2  
Old 11-25-2015
Hello greycells,

Following may help you same, could you please try it and let me know if this helps you.
Code:
awk '/^\/clusters\/*/{if(A){print A;A=""};gsub(/.*\/|\:/,X,$0);A=$0} /^initiators/{sub(/\[/,X,$2);gsub(/\, /,";",$0);A=A "," $0;while($0 !~ /operational-status/){sub(/^[[:space:]]+/,X,$0);gsub(/\]|\,$/,X,$0);gsub(/\,[[:space:]]+/,";",$0);A=A ";" $0;getline}} END{print A}' Input_file

Output will be as follows.
Code:
M1_CRE03_SV,initiators AM112_A;AM112_B;KEC12_C;ABL12_D;BL13_A,;initiators AM112_A;AM112_B;KEC12_C;ABL12_D;BL13_A;BL13_B;ZBL13_C;ZBL13_D;BL14_A;ZBL14_B;ZBL14_C;BL14_D
M1P5_T2_SV,initiators G105_A;CIG105_B;CIG105_C;CIG105_D];initiators G105_A;CIG105_B;CIG105_C;CIG105_D

EDIT: Adding a non-one liner form of solution.
Code:
awk '/^\/clusters\/*/{
                        if(A){
                                print A;
                                A=""
                             };
                        gsub(/.*\/|\:/,X,$0);
                        A=$0}
    /^initiators/    {
                        sub(/\[/,X,$2);
                        gsub(/\,[[:space:]]+/,";",$0);
                        A=A "," $0;
                        while($0 !~ /operational-status/){
                                                                sub(/^[[:space:]]+/,X,$0);
                                                                gsub(/\]|\,$/,X,$0);
                                                                gsub(/\,[[:space:]]+/,";",$0);
                                                                A=A ";" $0;
                                                                getline
                                                         }
                     }
    END{print A}
    '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-25-2015 at 01:34 PM.. Reason: Added a non one liner form of solution. Updated gsub values as per user's output and informed user too.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-25-2015
getting this ...

Code:
 
 M1_CRE03_SV AM112_A, initiators AM112_A, AM112_B, KEC12_C, ABL12_D, BL13_A, BL13_B, ZBL13_C, ZBL13_D, BL14_A, ZBL14_B, ZBL14_C, BL14_D
 M1P5_T2_SV G105_A, initiators G105_A, CIG105_B, CIG105_C, CIG105_D

# 4  
Old 11-25-2015
Another approach:-
Code:
gawk -F'[][,/:]' '
        /storage-views/ {
                name = $(NF-1)
                next
        }
        /^initiators/ {
                flag = 1
        }
        flag && ( !/^initiators/ && !/^ / ) {
                flag = 0
                print name "," value
                value = ""
        }
        flag {
                for ( i = 1; i <= NF; i++ )
                {
                        gsub(/ /,X,$i)
                        if ( $i !~ /initiators/ && $i )
                                value = value ? value OFS $i : $i
                }
        }
' OFS=\; file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 11-25-2015
Or
Code:
awk '
/:$/            {sub (/:/, ",")
                 n = split ($0, T, "/")
                 printf "%s", T[n]
                }
/^initia/       {while (!/]$/)  {getline X
                                 $0 = $0 X
                                }
                 sub ("^" $1, "")
                 gsub (/[][ ]*/, "")
                 gsub (/,/, ";")
                 print
                }
' file
M1_CRE03_SV,AM112_A;AM112_B;KEC12_C;ABL12_D;BL13_A;BL13_B;ZBL13_C;ZBL13_D;BL14_A;ZBL14_B;ZBL14_C;BL14_D
M1P5_T2_SV,G105_A;CIG105_B;CIG105_C;CIG105_D

This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-28-2015
Or:
Code:
awk -F '[][:]' '{gsub(/,[[:space:]]*/,";")} NF>1{print $1 "," $3}' RS=/ file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-29-2015
Hi Scrutinizer ..

I get this error
gawk: fatal: Unmatched [ or [^: /[][:]/


Do I need to escape something ?

thanks ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

2. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

3. UNIX for Dummies Questions & Answers

Des/awk for change format and adding integers in a column of data?

Greetings! I need a quick way to change the format in a table of data Here is an example of the input: 10 72 Value=177 VDB=0.0245 Value4=0,0,171,0 10 274 Value=238 VDB=0.0433 Value4=29,0,205,0 10 312 Value=222 VDB=0.0384 Value4=8,0,190,19 10 540 Value=405 VDB=0.0391 Value4=13,30,153,195... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

4. Shell Programming and Scripting

awk - script help: column to row format of data allignment?

Experts Good day, I have the following data, file1 BRAAGRP1 A2X B2X C2X D2X BRBGRP12 A3X B3X Z10 D09 BRC1GRP2 LO01 (4 Replies)
Discussion started by: rveri
4 Replies

5. Shell Programming and Scripting

Need help getting data into legible format...maybe awk?

Good morning, I am still learning the powers of awk and perl. I am in need of a bit of help. I have a script on one of my launch systems...if that is even the word for it. Basically you can only ssh to a system if you are connected to this system due to firewalls. So from that system, I... (2 Replies)
Discussion started by: brianjb
2 Replies

6. Shell Programming and Scripting

Arrange / format data using awk

Input 217:fngadi4osa:fngadi4osa:M 217:415744:N/A 227:fngadi4osa:fngadi4osa: M 227:51200:N/A 228:fngadi4osa:fngadi4osa: M 228:102400:N/A 65:sapgt04:sapgt04: M 65:104448:N/A 228:fngadi4osa:fngadi4oma: M 228:102400:N/A Output 217:fngadi4osa:fngadi4osa:M 217:415744:N/A... (3 Replies)
Discussion started by: greycells
3 Replies

7. Shell Programming and Scripting

Using Awk for extracting data in specific format

please help me writing a awk script 001_r.pdb 0.0265185 001_r.pdb 0.0437049 001_r.pdb 0.0240642 001_r.pdb 0.0310264 001_r.pdb 0.0200482 001_r.pdb 0.0146746 001_r.pdb 0.0351344 001_r.pdb 0.0347856 001_r.pdb 0.036119 001_r.pdb 1.49 002_r.pdb 0.0281011 002_r.pdb 0.0319908 002_r.pdb... (5 Replies)
Discussion started by: phoenix_nebula
5 Replies

8. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies

9. UNIX for Dummies Questions & Answers

converting a tabular format data to comma seperated data in KSH

Hi, Could anyone help me in changing a tabular format output to comma seperated file pls in K-sh. Its very urgent. E.g : username empid ------------------------ sri 123 to username,empid sri,123 Thanks, Hema:confused: (2 Replies)
Discussion started by: Hemamalini
2 Replies

10. Shell Programming and Scripting

data format from (4.56 0.7) -> 4.6(7) awk?!

Hi, I have to manipulate a data file which say reads like this {$index $value $error_on_value} aa 4.56 0.7 bb 123.456 0.00987 cc 987654 321 . . in easily human readable format of type aa 4.6(7) bb 123.456(1) cc 9.877(3)e+05 value rounded to 4.6 with error of 0.7 on the last... (4 Replies)
Discussion started by: ahan
4 Replies
Login or Register to Ask a Question