Create the space only in the Third column by awk

Hi All ,

I am having an input file like this

Input file

                           r                  s   
                           e                  y   
Pin Numbers                s                  n   
                           eppppppppppppppppppc   
                           taaaaaaaaaaaaaaaaaah   
                           _331111111100000000r   
                           n010145678989234567o   
pattern         offset  
scan0_core_p    2965       11101101010LLLLLHLL0   
                                           ^      
scan0_core_p    2967       11111010111HHLLLHLL0   
                                           ^      
scan0_core_p    2968       11110110011HLLLHHHL0   
                                           ^      
scan0_core_p    2976       11110111100LLHLLHHL0   
                                                   ^    
scan0_core_p    2977       11111011110LHHLLHLL0   
                                                ^    
scan0_core_p    2978       11100010110HHLLLHLL0   
                                             ^    

I want to create space in my Third column of the input file
and output should be something like this


Pin Numbers                                 s                                     n       
                                                   e p p p p p p p p p p p p p p p p p p c       
                                                   t a a a a a a a a a a a a a a a a a a h       
                                                   _ 3 3 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 r       
                                                   n 0 1 0 1 4 5 6 7 8 9 8 9 2 3 4 5 6 7 o       
pattern                   o f f s e t     
scan 0_core_ p             2965                    1 1 1 0 1 1 0 1 0 1 0 L L L L L H L L 0       
                                                                                  ^             
scan0_core_ p              2967                     1 1 1 1 1 0 1 0 1 1 1 H H L L L H L L 0    


I am using below stated script

awk 'BEGIN{FS=OFS=""}  {for (i=1;i<=NF;i++) {if ( i%1==0) $i=$i " "}}1' input.file 

But this script is changing the content in other columns also
Could you help me out ?

How about

awk 'NR == 1 {L = index ($0, "r") - 1} {T = substr ($0, L); gsub (/./, "& ", T);  print substr ($0, 1, L), T}' file
                              r                                     s       
                              e                                     y       
Pin Numbers                   s                                     n       
                              e p p p p p p p p p p p p p p p p p p c       
                              t a a a a a a a a a a a a a a a a a a h       
                              _ 3 3 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 r       
                              n 0 1 0 1 4 5 6 7 8 9 8 9 2 3 4 5 6 7 o       
pattern         offset   
scan0_core_p    2965          1 1 1 0 1 1 0 1 0 1 0 L L L L L H L L 0       
                                                              ^             
scan0_core_p    2967          1 1 1 1 1 0 1 0 1 1 1 H H L L L H L L 0       
                                                              ^             
scan0_core_p    2968          1 1 1 1 0 1 1 0 0 1 1 H L L L H H H L 0       
                                                              ^             
scan0_core_p    2976          1 1 1 1 0 1 1 1 1 0 0 L L H L L H H L 0       
                                                                              ^         
scan0_core_p    2977          1 1 1 1 1 0 1 1 1 1 0 L H H L L H L L 0       
                                                                        ^         
scan0_core_p    2978          1 1 1 0 0 0 1 0 1 1 0 H H L L L H L L 0       
                                                                  ^ 
1 Like

Hi Rudic ,

Thanks a lot , could you help me out with brief explanation of this script , It will help me out to counteract similar problems.

You present a difficult data structure that cannot be handled with the usual, generic,"normal" awk methods, like fields, field separators, data elements in one line. You have vertical "pin numbers" in varying fields, positional pointers into to a beforementioned line, no indicator where data begin in a line. That makes a somewhat non-standard approach necessary, relying on unconfirmed, unverified assumptions.

awk '
NR == 1         {L = index ($0, "r") - 1        # on the first line, calculate where the (all space) lead-in part ends (and 
                }                               # the (non-space) data begin)
                {T = substr ($0, L)             # save the "data part" of each line in a temp variable
                 gsub (/./, "& ", T)            # add a space to every single char in the temp var
                 print substr ($0, 1, L), T     # print the first, unmodified part of the line (= $1 and $2),
                }                               # then the "spaced" data part in temp var
' file