Parse input -AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse input -AWK
# 1  
Old 05-03-2012
Parse input -AWK

Input File


Code:
 
Defined configuration:
cfg:   CLL_DCC_Fabric_A        
                BTS00P21; BAU_AP00P01QC; BAU_LGSCNJP02; BAU_TS00P20; 
                BAU_DSMSM14; BAU_HT00P02; BAU_DSMSM13; BAU_HT00P01; 
cfg:   CX0014_list     
                BAU_TS00P20; BAU_NYP_PRODIAD1_CJ; BAU_NYP_FILESRV4_CJ; 
                BAU_NYP_DNET01_CJ; BAU_NYP_ERECIISA_CJ; BAU_FNGBPD1OSA; 
zone:  BAU_AE00T11     
                C0:50:76:01:C6:20:00:12; 50:06:01:69:47:20:07:FC; 
                50:06:01:60:47:20:07:FC
zone:  BAU_NYP_SHPTSQL2_CJ     
                10:00:00:00:C9:77:DC:80; 50:06:01:60:3C:A0:13:47; 
                50:06:01:69:3C:A0:13:47; 50:06:01:60:47:20:01:D4; 
                50:06:01:69:47:20:01:D4
alias: AE00T10_AB97    
                10:00:00:00:C9:8C:AB:97
alias: AI00P01_4B17    
                10:00:00:00:C9:4B:4B:17
Effective configuration:
cfg:   CLL_DCC_Fabric_A        
zone:  BAU_AE00T11     
                c0:50:76:01:c6:20:00:12
                50:06:01:69:47:20:07:fc
                50:06:01:60:47:20:07:fc
zone:  BAU_AE00T13     
                c0:50:76:01:c6:20:00:06
                50:06:01:69:47:20:07:fc
                50:06:01:60:47:20:07:fc

Output needed

Code:
 
Defined:CLL_DCC_Fabric_A:BAU_AE00T11,C0:50:76:01:C6:20:00:12,50:06:01:69:47:20:07:FC,50:06:01:60:47:20:07:FC
Defined:CLL_DCC_Fabric_A:BAU_NYP_SHPTSQL2_CJ,10:00:00:00:C9:77:DC:80,50:06:01:60:3C:A0:13:47,50:06:01:69:3C:A0:13:47,50:06:01:60:47:20:01:D4,50:06:01:69:47:20:01:D4
Effective:CLL_DCC_Fabric_A:BAU_AE00T11,c0:50:76:01:c6:20:00:12,50:06:01:69:47:20:07:fc,50:06:01:60:47:20:07:fc
Effective:CLL_DCC_Fabric_A:BAU_AE00T13,c0:50:76:01:c6:20:00:06,50:06:01:69:47:20:07:fc,50:06:01:60:47:20:07:fc

Only looking for the info in the "zone:" lines - Thx
# 2  
Old 05-04-2012
Have a go with this:

Code:
awk '
    BEGIN {
        defined = 0;
        effective = 1;
    }

    function p()
    {
        if( data != "" )
        {
            gsub( ",$", "", data );
            printf( "%s%s:%s,%s\n", mode == defined ? "Defined: " : "Effective:", ctype, ztype, data );
            ztype = ""
            data = "";
        }
    }

    /^Defined configuration:/       { ctype=""; mode = defined; next; }
    /^Effective configuration:/     { ctype=""; mode = effective; next; }
    /^cfg:/ && !ctype {  ctype = $2; next; }

    /^zone:/ {
        p( );           # print what we have and reset, capturing the type
        snarf = 1;
        ztype = $2;
        next;
    }

    /^alias:/ {          # print, reset and stop picking up data
        p( );
        snarf = 0;
        next;
    }

    snarf {             # collect data if enabled
        gsub( "^[ \t]+", "" );      # ditch lead whitespace
        gsub( "; ", ",", $0 );      # convert ; to commas
        data = data $0;             # add to the pile
        next;
    }

    END {               # last one if not printed
        p( );
    }
'  input-file >output-file


Last edited by agama; 05-04-2012 at 12:21 AM.. Reason: small typo
# 3  
Old 05-04-2012
Thanks for the quick response agama , but there is a slight issue ... its working OK for the defined part , but not for the effective part ... the effective part input is different ..the FS is a newline instead of;

Getting Output
Code:
 
Defined: CLL_DCC_Fabric_A:BAU_AE00T11,C0:50:76:01:C6:20:00:12,50:06:01:69:47:20:07:FC,50:06:01:60:47:20:07:FC
Defined: CLL_DCC_Fabric_A:BAU_NYP_SHPTSQL2_CJ,10:00:00:00:C9:77:DC:80,50:06:01:60:3C:A0:13:47,50:06:01:69:3C:A0:13:47,50:06:01:60:47
:20:01:D4,50:06:01:69:47:20:01:D4
Effective:CLL_DCC_Fabric_A:BAU_AE00T11,c0:50:76:01:c6:20:00:1250:06:01:69:47:20:07:fc50:06:01:60:47:20:07:fc
Effective:CLL_DCC_Fabric_A:BAU_AE00T13,c0:50:76:01:c6:20:00:0650:06:01:69:47:20:07:fc50:06:01:60:47:20:07:fc

Desired Output
Code:
 
Defined: CLL_DCC_Fabric_A:BAU_AE00T11,C0:50:76:01:C6:20:00:12,50:06:01:69:47:20:07:FC,50:06:01:60:47:20:07:FC
Defined: CLL_DCC_Fabric_A:BAU_NYP_SHPTSQL2_CJ,10:00:00:00:C9:77:DC:80,50:06:01:60:3C:A0:13:47,50:06:01:69:3C:A0:13:47,50:06:01:60:47
:20:01:D4,50:06:01:69:47:20:01:D4
Effective:CLL_DCC_Fabric_A:BAU_AE00T11,c0:50:76:01:c6:20:00:12,50:06:01:69:47:20:07:fc,50:06:01:60:47:20:07:fc
Effective:CLL_DCC_Fabric_A:BAU_AE00T13,c0:50:76:01:c6:20:00:06,50:06:01:69:47:20:07:fc,50:06:01:60:47:20:07:fc

# 4  
Old 05-04-2012
Oops. Small change:

Code:
awk '
    BEGIN {
        defined = 0;
        effective = 1;
    }

    function p()
    {
        if( data != "" )
        {
            gsub( ",$", "", data );
            printf( "%s%s:%s,%s\n", mode == defined ? "Defined: " : "Effective:", ctype, ztype, data );
            ztype = ""
            data = "";
        }
    }

    /^Defined configuration:/       { ctype=""; mode = defined; next; }
    /^Effective configuration:/     { ctype=""; mode = effective; next; }
    /^cfg:/ && !ctype {  ctype = $2; next; }

    /^zone:/ {
        p( );           # print what we have and reset, capturing the type
        snarf = 1;
        ztype = $2;
        next;
    }

    /^alias:/ {             # print, reset and stop picking up data
        p( );
        snarf = 0;
        next;
    }

    snarf {             # collect data if enabled
        gsub( "^[ \t]+", "" );      # ditch lead whitespace
        gsub( ";", "", $0 );        # drop ;
        gsub( " ", ",", $0 );       # convert add commas
        data = data $0 ",";             # add to the pile
        next;
    }

    END {               # last one if not printed
        p( );
    }
'

This User Gave Thanks to agama For This Post:
# 5  
Old 05-04-2012
Alternatively:
Code:
awk '$0~"^"$1{print x}1' infile |
awk '/^Defined|^Effective/{t=$1":"} $1=="cfg:" && $2~/^CLL_/{m=$2":"} /^zone/{$1=t m;sub(OFS,x);gsub(/;/,x); print}' RS= OFS=,

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 05-04-2012
Quote:
Originally Posted by Scrutinizer
Alternatively:
Code:
awk '$0~"^"$1{print x}1' infile |
awk '/^Defined|^Effective/{t=$1":"} $1=="cfg:" && $2~/^CLL_/{m=$2":"} /^zone/{$1=t m;sub(OFS,x);gsub(/;/,x); print}' RS= OFS=,

I almost added a comment that Scrutinizer will surely post something much more compact Smilie
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Parse input of two files to be the same in awk

I have two files that I am going to use diff to find the differences but need to parse them before I do that. I have include the format of each file1 and file2 with the desired output of each (the first 5 fields in each file). The first file has a "chr" before the # that needs to be removed. I... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Parse a file using awk

Hi Experts, I am trying to parse the following file; FILEA a|b|c|c|c|c a|b|d|d|d|d e|f|a|a|a|a e|f|b|b|b|boutput expected: a<TAB>b <TAB><TAB>c<TAB>c<TAB>c<TAB>c<TAB> <TAB><TAB>d<TAB>d<TAB>d<TAB>d<TAB> e<TAB>f <TAB><TAB>a<TAB>a<TAB>a<TAB>a<TAB> <TAB><TAB>b<TAB>b<TAB>b<TAB>b<TAB>*... (7 Replies)
Discussion started by: rajangupta2387
7 Replies

4. Shell Programming and Scripting

Parse find input into array

I need help parsing the output of find into an array. I need to search 3 directories and find all files older than 31 days old. This is what I have so far. TIME=" -maxdepth 1 -mtime +31" DIR1="/dir1/" DIR2="/dir2/" DIR3="/dir3/" FIND_DIR1=$(find ${DIR1}${TIME}) FIND_DIR3=$(find... (8 Replies)
Discussion started by: jrymer
8 Replies

5. Shell Programming and Scripting

Bash Script for parse input like option and value

I would create a bash script than parse like this: test.sh -p (protocol) -i (address) -d (directory) I need retrive the value after -p for example... understand??? I hope... thanks (6 Replies)
Discussion started by: ionral
6 Replies

6. Shell Programming and Scripting

Parse a file with awk?

Hi guys (and gals). I need some help. I'm running an IVR purely on Asterisk where I capture the DTMFs. After pulsing each DTMF I have Asterisk write to a file with whatever was dialed (mostly used for record-keeping) and at the end of the survey I write all variables in a single line to a... (2 Replies)
Discussion started by: tulf210
2 Replies

7. Shell Programming and Scripting

Parse file using awk and work in awk output

hi guys, i want to parse a file using public function, the file contain raw data in the below format i want to get the output like this to load it to Oracle DB MARWA1,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 MARWA2,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 this the file raw format: Number of... (6 Replies)
Discussion started by: dagigg
6 Replies

8. Shell Programming and Scripting

parse long input parameters

anybody know a nice way to parse long input parameters such as --path /dir1/dir2/ (see below). Now I have more than 10 input parameters and it's confusing having parameters like -q something, I would prefer longer ones case $OPTKEY in --path) M_PATH=$OPTARG ;; -s) ... (3 Replies)
Discussion started by: larne
3 Replies

9. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

10. Shell Programming and Scripting

AWK unable to parse

awk -v new=" " ' substr($0, 17, 3) == "ABC" && substr($0, 52, 8) == "00000000" { tr=substr($0, 20, 10); ap=substr($0, 30, 2); ver=substr($0, 32, 2); irver=substr($0, 34, 2); ... (12 Replies)
Discussion started by: COD
12 Replies
Login or Register to Ask a Question