Single liner awk command required

Dear team,

Here is my logfile

    SP             SPID     SPSTATE     BROADCASTSTATUS  SCCPSTATE 
2-11           602GSU1  ALLOWED     CON              ALLOWED   
                                                               
                        SSN         SUBSYSTEMSTATE   SST       
                        146         ALLOWED          YES       
                        251         ALLOWED          YES       
                                                               
SP             SPID     SPSTATE     BROADCASTSTATUS  SCCPSTATE 
2-12           601GSU2  ALLOWED     CON              ALLOWED   
                                                               
                        SSN         SUBSYSTEMSTATE   SST       
                        NONE                                   
2-13           602GSU1  ALLOWED     CON              ALLOWED   
                                                               
                        SSN         SUBSYSTEMSTATE   SST       
                        146         ALLOWED          YES       
                        251         ALLOWED          YES       
                                                               
SP             SPID     SPSTATE     BROADCASTSTATUS  SCCPSTATE 
2-14           601GSU2  ALLOWED     CON              ALLOWED   
                                                               
                        SSN         SUBSYSTEMSTATE   SST       
                        NONE                                   

I need to process output for this file in such a way that each blog of data appear in single line as below

    SP       SPID     SPSTATE    BCSTATE SCCPSTATE  SSN SUBSTATE   SST
2-11     602GSU1  ALLOWED    NCON    ALLOWED
2-12    601GSU2  ALLOWED    NCON    ALLOWED
2-13    601GSU1  ALLOWED    NCON    ALLOWED
2-14    601GSU2  ALLOWED    NCON    ALLOWED

Here is my parse script file

    BEGIN {
    split("SP SPID SPSTATE BCSTATE SCCPSTATE SSN SUBSTATE SST",array," ");
    printf("%-9s", array[1])
    printf("%-9s", array[2])
    printf("%-11s",array[3])
    printf("%-8s", array[4])
    printf("%-11s",array[5])
    printf("%-4s", array[6])
    printf("%-11s",array[7])
    printf("%-4s", array[8])
    printf("\n");
}

/^[0-9]/  { array[1]=(substr($0,1,7))   #SP
            array[2]=(substr($0,16,7))  #SPID 
            array[3]=(substr($0,25,10)) #SPSTATE
            array[4]=(substr($0,37,6))  #BROADCASTSTATUS
            array[5]=(substr($0,54,10)) #SCCPSTATE
            print_array1(array)
}
/SSN / {    getline
            while(NF>1)
            {
               array[6]=(substr($0,25,4)) #SSN
               array[7]=(substr($0,37,10))#SUBSYSTEMSTATE
               array[8]=(substr($0,54,3)) #SST
               print_array2(array)
               getline
            }
       }
END{}

function print_array1(array)
{
    printf("%-9s", array[1])
    printf("%-9s", array[2])
    printf("%-11s",array[3])
    printf("%-8s", array[4])
    printf("%-11s",array[5])
    printf("\n");
    for(item=3;item<=5;item++){array[item]=""}
}

function print_array2(array)
{
    printf("%-9s", array[1])
    printf("%-9s", array[2])
    printf("%33s ", array[6])
    printf("%-11s", array[7])
    printf("%-4s", array[8])
    printf("\n");
    for(item=6;item<=8;item++){array[item]=""}
}

This while executing as below giving me same result as i need.

    awk -f test_parsa.awk test.log

My concern is i want same above awk execution in single line without using this test_parsa.awk file. convert test_parsa.awk file in awk single liner.

I tried this as well but giving me below error

    awk: cmd. line:1: BEGIN{split("SP SPID SPSTATE BCSTATE SCCPSTATE SSN SUBSTATE SST",array," ") print a["i"],a["z"],a["x"],a["y"],a["o"],a["p"],a["q"],a["r"] }/^[0-9]/{a["i"]=substr($0,1,7);a["z"]=substr($0,16,7);a["x"]=substr($0,25,10);a["y"]=substr($0,37,6);a["o"]=substr($0,54,10);print_ar1(a)}/SSN /{getline;while(NF>1){a["p"]=substr($0,25,4);a["q"]=substr($0,37,10);a["r"]=substr($0,54,3);print_ar2[a];getline}}END{print_ar1(a){print a["i"],a["z"],a["x"],a["y"],a["o"];for(item=3;item<=5;item++){array[item]=""}}{print_ar2(a){print a["p"],a["q"],a["r"];for(item=6;item<=8;item++){array[item]=""}}}
awk: cmd. line:1:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ^ unexpected newline or end of string

Thanks

Please use markdown language when posting code and sample input out put text.

Normally, this is done by using three back ticks for each code block.

See this post for details - Please Refrain from Using Single Backticks for Markdown

Dear Neo,

Thanks for comment.

Really appreciate if provide any example for markdown language. I didnt get this point. Even from the link as well.

Here ya go (but you can Google and find more examples, this is only one)

This might help you as well (from the above cheat sheet):

Hi
Maybe you forgot to put single quotes. Try this

cat test_parsa.awk

Then you copy the output to the buffer with the mouse.
After the next line:

awk '

Paste the buffer via the mouse.
And append a single quote and file name

' test.log

and press ENTER
Together, the following command will be executed

eval awk "'$(< test_parsa.awk)'" test.log

Hope this helps to understand your mistake.

To add:
If you do not specify language then one will be guessed for you,
so syntax highlighting will occur then, but possibly not the right one.

If you use the following:

```text
text indicated so no syntax highlighting
```

Then no syntax highlighting will occur, which in many cases is preferable.

2 Likes

I have done a lot of editing of others posts recently, changing their "colorful" syntax highlighting code tags to:

```text
text indicated so no syntax highlighting
```

You can also use this composer icon (which I created during migration)..... instead of typing the markdown:

1 Like

Thanks Neo

1 Like