Transpose info that is within blocks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose info that is within blocks
# 1  
Old 03-24-2012
Transpose info that is within blocks

Hello to all in forum,

I have a big file with blocks of data. Each block begins with "BeginOfRecord". There are blank lines
separating each block. I would like to transpose the info within each block in order to have each
blockīs info in a single line using awk but I don't know how to do it, I hope some awk expert could help me.

The input file looks like this:

Code:
 BeginOfRecord                                                 x( 0)-(SEQUENCE)   -> 000000:              
   A0820133                                                                                                 
      MSTerminating                                                x( 4)-(SEQUENCE)   -> 000004:              
      A482012F                                                                                                
        ChargeableDuration                                       x( 13)-(PARENT)     -> 00000C:             
        8D03                                                                                                
          .Time                                                  u( -1)-(BYTE)                              
            .Hour                       : 00                     k( -1)-(INT)        -> 00000E:             
            00                                                                                              
            .Minute                     : 01                     k( -1)-(INT)        -> 00000F:             
            01                                                                                              
            .Second                     : 11                     k( -1)-(INT)        -> 000010:             
            0B                                                                                              
        RecordSeqNumber                 : 123DED                 x( 2)-(BYTE)       -> 00002C:                     
        DateForStartOfCharge                                     x( 10)-(PARENT)     -> 000011:             
        8A03                                                                                                
          .Date                         : 20120313               k( -1)-(DATE)       -> 000013:             
          0C030D                                                                                            
        ExIdentity                      : ABRMMG2_ARI11_L        x( 10)-(ASCII)      -> 00091F:             
        EMLPPPriorityLevel              : 01                     x( 74)-(BYTE)       -> 000130:             
        9F4A0101                                                                                                     
                                                                                                            
                                                                                                            
   BeginOfRecord                                                 x( 0)-(SEQUENCE)   -> 000914:              
   A081A9                                                                                                   
      SSProcedure                                              x( 7)-(SEQUENCE)   -> 000917:                
      A781A6                                                                                                
        DateForStartOfCharge                                     x( 6)-(PARENT)     -> 00091A:              
        8603                                                                                                
          .Date                         : 20120207               k( -1)-(DATE)       -> 00091C:             
          0C030D                                                                                            
        ExIdentity                      : KULMMG5_ARI56_T        x( 10)-(ASCII)      -> 00091F:

The output I would like is as follow:

Code:
Type Of Record|ChargeableDuration|RecordSeqNumber|DateForStartOfCharge|ExIdentity                      
  MSTerminating|00:01:11|123DED|20120313|ABRMMG2_ARI11_L                                                 
  SSProcedure|||20120207||KULMMG5_ARI56_T

As you can notice, the texts in left side of input file become in headers (like 1rst line in blue) and some of those values
not always appears in all blocks, then if a value is not present the corresponding column will be blank, like in 3rd line.

Many thanks in advance for any help.
# 2  
Old 03-24-2012
Hi, is there something you tried so far?
# 3  
Old 03-24-2012
Code:
nawk '/BeginOfRecord/{getline ;getline ;x=$1"|"}/(Hour|Minute|Second)/{x=x (($0~/Hour/)?z:":")$3}/(RecordSeqNumber|\.Date|ExIdentity)/{x=x"|"$3}/ExIdentity/{print x}' yourfile

---------- Post updated at 10:15 AM ---------- Previous update was at 09:50 AM ----------

To always have the same number of fields
Code:
awk '
/BeginOfRecord/{getline;getline;R=$1}  
/Hour/{T=$3}/(Minute|Second)/{T=T":"$3}
/RecordSeqNumber/{N=$3}
/\.Date/{D=$3}
/ExIdentity/{print R OFS T OFS N OFS D OFS $3;R=T=N=D=z}' OFS="|" yourfile

Code:
awk '
/BeginOfRecord/{getline ;getline ;R=$1}
$2==":"{a[$1]=$3}
/ExIdentity/{
h=a[".Hour"]
T=length(h)?h":"a[".Minute"]":"a[".Second"]:z
print R OFS T OFS a["RecordSeqNumber"] OFS a[".Date"] OFS a[$1]
split(z,a)
}' OFS="|" infile


Last edited by ctsgnb; 03-24-2012 at 07:34 AM..
# 4  
Old 03-24-2012
Alternative approach including header you could try:
Code:
awk '{$1=$1}1' infile |
awk '
BEGIN{
  header="Type Of Record|ChargeableDuration|RecordSeqNumber|DateForStartOfCharge|ExIdentity"
  print header
  m=split(header,H,"|")
}
{ 
  $1=$7; $2=$3=$4=$5=x 
  for(i=8;i<=NF;i++) {
    if($i==H[2]) $2=$(i+11)":"$(i+19)":"$(i+27)
    if($i==H[3]) $3=$(i+2)
    if($i==H[4]) $4=$(i+8)
    if($i==H[5]) $5=$(i+2)
  }
  NF=m
}1
' RS= OFS=\|


Last edited by Scrutinizer; 03-24-2012 at 07:22 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-24-2012
Code:
awk ' BEGIN{
      b="BeginOfRecord" 
      h=".Hour";m=".Minute";s=".Second" 
      n="RecordSeqNumber" 
      d=".Date" 
      X="ExIdentity"
      print b"|"h":"m":"s"|"n"|"d"|"X
} 
$0~b{getline;getline;R=$1} 
$2==":"{a[$1]=$3} 
$0~X{T=a[h]":"a[m]":"a[s]
      print R OFS (T!="::"?T:z) OFS a[n] OFS a[d] OFS a[X];split(z,a)
}' OFS=\| infile

This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 03-24-2012
Hello Scrutinizer and ctsgnb,

Thanks for your reply and help.

I've tried your solutions.

Scrutnitzer,

I couls understand your script but I think I don't understand it completely, I've modified to get all columns I want for a real file and some values doesn't appear for some lines. The goal is that if in a block appear all values the transposed line must contain all values that I want to print, if a block only contains certain values, the corresponding column must be empty. Below is a excerpt of a real inputfile that contains much more parameters that will be headers in the ourput (the real file contain many blocks and 1 million lines).

The modified version is as below:
Code:
Modified version of Scrutinizer script
awk '{$1=$1}1' inputfile |
awk '
BEGIN{
  header="Type Of Record|DateForStartOfCharge|ChargeableDuration|RecordSequenceNumber|ExchangeIdentity|TimeForStartOfCharge|TimeForStopOfCharge|GSMCallReferenceNumber|CallReferenceNumber|MSCAddress|CallingPartyNumber|CalledPartyNumber"
  print header
  m=split(header,H,"|")
}
{ 
  $1=$7; $2=$3=$4=$5=$6=$7=$8=$9=$10=$11=$12=x 
  for(i=8;i<=NF;i++) {
    if($i==H[2]) $2=$(i+8)
    if($i==H[3]) $3=$(i+11)":"$(i+19)":"$(i+27)
    if($i==H[4]) $4=$(i+2)
    if($i==H[5]) $5=$(i+2)
    if($i==H[6]) $6=$(i+11)":"$(i+19)":"$(i+27)
    if($i==H[7]) $7=$(i+11)":"$(i+19)":"$(i+27)
    if($i==H[8]) $8=$(i+2)
    if($i==H[9]) $9=$(i+2)
    if($i==H[10]) $10=$(i+22) 
    if($i==H[11]) $11=$(i+22)  
    if($i==H[12]) $11=$(i+22)   
  }
  NF=m
}1
' RS= OFS=\| > Output.csv

Hello ctsgnb,

I've tried your scripts, the 1rst and 3rd work the 2nd I get errors. The thing is I don't understand your script too much to modifiy them in order to print the corresponding values for each parameter in red above.

The larger sample below shows only 2 blocks then the output would containd 2 lines plus the header (3 in total)
(the parameters I want to get the values from are in blue, the corresponding values I want from each parameter are in red )
Code:
  *** BeginOfRecord                                                                                                                                                                                     
    Filepos: 0 (0x00)                                                                                                                                                                                   
    Size   : 111 (0x117)                                                                                                                                                                                
    Input record number : 1                                                                                                                                                                             
    Output record number: 1                                                                                                                                                                             
                                                                                                                                                                                                        
    BeginOfRecord                                                c(  0)-(SEQUENCE)   -> 000000: A0820111                                                                                                
      MSTerminating                                              c(  4)-(SEQUENCE)   -> 000004: A482012F                                                                                                
        CallPosition                    : 01                     c( 11)-(INT)        -> 000008: 2F210101                                                                                                
        ChargeableDuration                                       c( 11)-(PARENT)     -> 00000C: 8D01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 00                     v( -1)-(INT)        -> 00000E: 00                                                                                                      
            .Minute                     : 01                     v( -1)-(INT)        -> 00000F: 01                                                                                                      
            .Second                     : 11                     v( -1)-(INT)        -> 000010: 0B                                                                                                      
        DateForStartOfCharge                                     c( 10)-(PARENT)     -> 000011: 8A01                                                                                                    
          .Date                         : 20120111               v( -1)-(DATE)       -> 000011: 0C010D                                                                                                  
        ExchangeIdentity                : ABRTTZ1_EMU57_J        c( 20)-(ASCII)      -> 000016: 240F5447554D4741115F42504111125F56                                                                      
        InterruptionTime                : 000000                 c( 14)-(BYTE)       -> 000027: 8E01000000                                                                                              
        RecordSequenceNumber            : 121DED                 c(  2)-(BYTE)       -> 00002C: 8201121DED                                                                                              
        TariffClass                     : 0001                   c( 18)-(BYTE)       -> 000011: 22020001                                                                                                
        TariffSwitchInd                 : 00                     c( 12)-(INT)        -> 000015: 210100                                                                                                  
        TimeForStartOfCharge                                     c( 11)-(PARENT)     -> 000018: 8B01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 10                     v( -1)-(INT)        -> 00001A: 0A                                                                                                      
            .Minute                     : 12                     v( -1)-(INT)        -> 00001B: 20                                                                                                      
            .Second                     : 57                     v( -1)-(INT)        -> 00001C: 12                                                                                                      
        TimeForStopOfCharge                                      c( 12)-(PARENT)     -> 00001D: 8C01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 10                     v( -1)-(INT)        -> 00001F: 0A                                                                                                      
            .Minute                     : 14                     v( -1)-(INT)        -> 000040: 22                                                                                                      
            .Second                     : 02                     v( -1)-(INT)        -> 000041: 02                                                                                                      
        OutputType                      : 04                     c(102)-(INT)        -> 000042: 2F660104                                                                                                
        SwitchIdentity                  : 0001                   c( 66)-(BYTE)       -> 000046: 2F42020001                                                                                              
        CalledSubscriberIMSI            : 111770018546158        c(  6)-(DIGIT)      -> 00004B: 860801117710586451F8                                                                                    
        CellIDFor1stCellCalled                                   c( 27)-(PARENT)     -> 000055: 2B07                                                                                                    
          .MCCMNC                       : 111 77                 v( -1)-(DIGIT)      -> 000057: 07F820                                                                                                  
          .MCC                          : 111                    i( -1)-(DETAIL)                                                                                                                        
          .MNC                          : 02                     i( -1)-(DETAIL)                                                                                                                        
          .LAC                          : 0015                   v( -1)-(BYTE)       -> 00005A: 0015                                                                                                    
          .CI                           : 012E                   v( -1)-(BYTE)       -> 00005C: 012E                                                                                                    
        CalledSubscriberIMEISV          : 1518440487441118       c(116)-(DIGIT)      -> 00005E: 2F74085181444078441181                                                                                  
        CalledSubscriberIMEI            : 151844048744110        c(  7)-(DIGIT)      -> 000062: 811151814440784411F0                                                                                    
        TimeForTCSeizureCalled                                   c( 26)-(PARENT)     -> 000071: 2A01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 10                     v( -1)-(INT)        -> 000075: 0A                                                                                                      
            .Minute                     : 12                     v( -1)-(INT)        -> 000076: 20                                                                                                      
            .Second                     : 54                     v( -1)-(INT)        -> 000077: 16                                                                                                      
        MobileStationRoamingNumber                               c(  8)-(PARENT)     -> 000078: 8807                                                                                                    
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 00007A: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 00007A: 11                                                                                                    
          .Number                       : 77742744687            v( -1)-(DIGIT)      -> 00007B: 0524724486F7                                                                                            
        EosInfo                         : 00                     c( 14)-(BYTE)       -> 000081: 2F220100                                                                                                
        GSMTeleServiceCode              : 11                     c( 22)-(BYTE)       -> 000085: 2D0111                                                                                                  
        FrequencyBandSupported          : 00                     c( 68)-(BYTE)       -> 000088: 2F440100                                                                                                
        MSCIdentification                                        c( 21)-(PARENT)     -> 00008C: 2507                                                                                                    
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 00008E: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 00008E: 11                                                                                                    
          .Number                       : 77745262511            v( -1)-(DIGIT)      -> 00008F: 0524252615F1                                                                                            
        TerminatingLocationNumber                                c( 25)-(PARENT)     -> 000025: 2207                                                                                                    
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 000027: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 000027: 11                                                                                                    
          .Number                       : 77745220102            v( -1)-(DIGIT)      -> 000028: 0524250201F2                                                                                            
        GSMCallReferenceNumber          : 464C6E0001             c( 71)-(BYTE)       -> 00002E: 2F4205464C6E0001                                                                                        
        MSCAddress                                               c( 65)-(PARENT)     -> 0000A6: 2F4107                                                                                                  
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 0000A2: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 0000A2: 11                                                                                                    
          .Number                       : 77745262512            v( -1)-(DIGIT)      -> 0000AA: 0524252615F2                                                                                            
        OutgoingRoute                   : MK1B7CO                c( 22)-(ASCII)      -> 0000B0: 26075451114217414F                                                                                      
        CalledPartyNumber                                        c(  5)-(PARENT)     -> 0000B2: 8507                                                                                                    
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 0000BB: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 0000BB: 11                                                                                                    
          .Number                       : 77748710617            v( -1)-(DIGIT)      -> 0000BC: 0524780116F7                                                                                            
        NetworkCallReference            : 5A4A170001             c( 67)-(BYTE)       -> 0000C2: 2F41055A4A170001                                                                                        
        CallingPartyNumber                                       c(  4)-(PARENT)     -> 0000CA: 8405                                                                                                    
          .TON                          : 4                      v( -1)-(MASK:41210000)-> 0000CC: 41                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 0000CC: 41                                                                                                    
          .Number                       : 22618102               v( -1)-(DIGIT)      -> 0000CD: 22161820                                                                                                
        IncomingRoute                   : MK1MK4I                c( 21)-(ASCII)      -> 0000D1: 254554511154511442                                                                                      
        PresentationAndScreeningIndicator : 10                   c( 57)-(BYTE)       -> 0000DA: 2F120110                                                                                                
        RedirectionCounter              : 00                     c( 18)-(BYTE)       -> 0000DE: 2F260100                                                                                                
        RelatedCallNumber               : 22AE2A                 c( 48)-(BYTE)       -> 0000E2: 2F100122AE2A                                                                                            
        CallIdentificationNumber        : 22AE2B                 c(  1)-(BYTE)       -> 0000E8: 810122AE2B                                                                                              
        TypeOfCallingSubscriber         : 01                     c(  1)-(INT)        -> 0000ED: 810101                                                                                                  
        RadioChannelProperty            : 01                     c( 58)-(INT)        -> 0000F0: 2F1A0101                                                                                                
        TAC                             : 01020E                 c(  0)-(BYTE)       -> 0000F4: 800101020E                                                                                              
        SubscriptionType                : 0B                     c( 61)-(BYTE)       -> 0000F2: 2F1F010B                                                                                                
        OriginForCharging               : 00                     c( 17)-(BYTE)       -> 0000FD: 210100                                                                                                  
        ChargedParty                    : 00                     c( 16)-(INT)        -> 000100: 200100                                                                                                  
        TimeFromRegisterSeizureToStartOfCharging                 c( 15)-(PARENT)     -> 000101: 8F01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 00                     v( -1)-(INT)        -> 000105: 00                                                                                                      
            .Minute                     : 00                     v( -1)-(INT)        -> 000106: 00                                                                                                      
            .Second                     : 06                     v( -1)-(INT)        -> 000107: 06                                                                                                      
        InternalCauseAndLoc             : 0001                   c( 15)-(BYTE)       -> 000108: 2F21020001                                                                                              
        FirstRadioChannelUsed           : 00                     c( 12)-(INT)        -> 00010D: 2F200100                                                                                                
        FirstAssignedSpeechCoderVersion : 2                      c( 61)-(INT)        -> 000111: 2F1D0102                                                                                                
        SpeechCoderPreferenceList       : 0201000501             c( 62)-(BYTE)       -> 000115: 2F1E050201000501                                                                                        
        CellIDForLastCellCalled                                  c( 28)-(PARENT)     -> 00011D: 2C07                                                                                                    
          .MCCMNC                       : 111 77                 v( -1)-(DIGIT)      -> 00011F: 07F820                                                                                                  
          .MCC                          : 111                    i( -1)-(DETAIL)                                                                                                                        
          .MNC                          : 02                     i( -1)-(DETAIL)                                                                                                                        
          .LAC                          : 0015                   v( -1)-(BYTE)       -> 000122: 0015                                                                                                    
          .CI                           : 01EA                   v( -1)-(BYTE)       -> 000124: 01EA                                                                                                    
        OutgoingAssignedRoute           : MK1B7CO                c( 78)-(ASCII)      -> 000126: 2F4E075451114217414F                                                                                    
        EMLPPPriorityLevel              : 01                     c( 74)-(BYTE)       -> 000110: 2F4A0101                                                                                                
        DisconnectingParty              : 00                     c(  2)-(INT)        -> 000114: 820100                                                                                                  
  *** Record is written                                                                                                                                                                                 
                                                                                                                                                                                                        
                                                                                                                                                                                                        
  *** BeginOfRecord                                                                                                                                                                                     
    Filepos: 111 (0x117)                                                                                                                                                                                
    Size   : 125 (0x145)                                                                                                                                                                                
    Input record number : 2                                                                                                                                                                             
    Output record number: 2                                                                                                                                                                             
                                                                                                                                                                                                        
    BeginOfRecord                                                c(  0)-(SEQUENCE)   -> 000117: A0820141                                                                                                
      MSOriginating                                              c(  1)-(SEQUENCE)   -> 00011B: A182011D                                                                                                
        CallPosition                    : 01                     c( 11)-(INT)        -> 00011F: 2F210101                                                                                                
        ChargeableDuration                                       c( 12)-(PARENT)     -> 000141: 8C01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 00                     v( -1)-(INT)        -> 000145: 00                                                                                                      
            .Minute                     : 00                     v( -1)-(INT)        -> 000146: 00                                                                                                      
            .Second                     : 12                     v( -1)-(INT)        -> 000147: 0C                                                                                                      
        DateForStartOfCharge                                     c(  2)-(PARENT)     -> 000148: 8201                                                                                                    
          .Date                         : 20120111               v( -1)-(DATE)       -> 00014A: 0C010D                                                                                                  
        ExchangeIdentity                : YYTTTZ1_EMU11_M        c( 20)-(ASCII)      -> 00014D: 240F5447554D4741115F42504111125F56                                                                      
        InterruptionTime                : 000000                 c( 11)-(BYTE)       -> 00015E: 8D01000000                                                                                              
        RecordSequenceNumber            : 801AB0                 c(  2)-(BYTE)       -> 000161: 8201121DEE                                                                                              
        TariffClass                     : 0001                   c( 18)-(BYTE)       -> 000168: 22020001                                                                                                
        TariffSwitchInd                 : 00                     c( 12)-(INT)        -> 00016C: 210100                                                                                                  
        TimeForStartOfCharge                                     c( 10)-(PARENT)     -> 00016F: 8A01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 10                     v( -1)-(INT)        -> 000171: 0A                                                                                                      
            .Minute                     : 11                     v( -1)-(INT)        -> 000172: 21                                                                                                      
            .Second                     : 57                     v( -1)-(INT)        -> 000171: 12                                                                                                      
        TimeForStopOfCharge                                      c( 11)-(PARENT)     -> 000174: 8B01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 10                     v( -1)-(INT)        -> 000176: 0A                                                                                                      
            .Minute                     : 14                     v( -1)-(INT)        -> 000177: 22                                                                                                      
            .Second                     : 02                     v( -1)-(INT)        -> 000178: 02                                                                                                      
        OutputType                      : 04                     c(102)-(INT)        -> 000172: 2F660104                                                                                                
        SwitchIdentity_MOC              : 0001                   c( 67)-(BYTE)       -> 00017D: 2F41020001                                                                                              
        MSCIdentification                                        c( 21)-(PARENT)     -> 000182: 2507                                                                                                    
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 000184: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 000184: 11                                                                                                    
          .Number                       : 77745262511            v( -1)-(DIGIT)      -> 000185: 0524252615F1                                                                                            
        CallingSubscriberIMEI           : 677428041128700        c(  6)-(DIGIT)      -> 00018B: 860851878240118207F0                                                                                    
        CallingSubscriberIMEISV         : 6774280411287001       c(141)-(DIGIT)      -> 000125: 2F810F085187824011820710                                                                                
        CallingSubscriberIMSI           : 111770012511615        c(  5)-(DIGIT)      -> 0001A1: 850801117710521116F5                                                                                    
        GSMTeleServiceCode              : 11                     c( 22)-(BYTE)       -> 0001AB: 2D0111                                                                                                  
        INMarkingOfMS                   : 5                      c( 47)-(INT)        -> 0001AE: 2F2F0105                                                                                                
        CellIDFor1stCellCalling                                  c( 27)-(PARENT)     -> 0001B2: 2B07                                                                                                    
          .MCCMNC                       : 111 77                 v( -1)-(DIGIT)      -> 0001B4: 07F820                                                                                                  
          .MCC                          : 111                    i( -1)-(DETAIL)                                                                                                                        
          .MNC                          : 02                     i( -1)-(DETAIL)                                                                                                                        
          .LAC                          : 001A                   v( -1)-(BYTE)       -> 0001B7: 001A                                                                                                    
          .CI                           : 068B                   v( -1)-(BYTE)       -> 0001B2: 068B                                                                                                    
        CalledPartyNumber                                        c(  7)-(PARENT)     -> 0001BB: 8701                                                                                                    
          .TON                          : 2                      v( -1)-(MASK:41210000)-> 0001BD: 21                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 0001BD: 21                                                                                                    
          .Number                       : B20                    v( -1)-(DIGIT)      -> 0001BE: 2BF0                                                                                                    
        FrequencyBandSupported          : 06                     c( 62)-(BYTE)       -> 0001C0: 2F450106                                                                                                
        GSMCallReferenceNumber          : 5A82640001             c( 85)-(BYTE)       -> 0001C4: 2F55055A82640001                                                                                        
        MSCAddress                                               c( 86)-(PARENT)     -> 0001CC: 2F5607                                                                                                  
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 0001CF: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 0001CF: 11                                                                                                    
          .Number                       : 77745262511            v( -1)-(DIGIT)      -> 0001D0: 0524252615F1                                                                                            
        OriginatingLocationNumber                                c( 25)-(PARENT)     -> 0001D6: 2207                                                                                                    
          .TON                          : 1                      v( -1)-(MASK:41210000)-> 0001D8: 11                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 0001D8: 11                                                                                                    
          .Number                       : 77745211178            v( -1)-(DIGIT)      -> 0001D2: 0524251171F8                                                                                            
        TimeForTCSeizureCalling                                  c( 26)-(PARENT)     -> 0001DF: 2A01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 10                     v( -1)-(INT)        -> 0001E1: 0A                                                                                                      
            .Minute                     : 11                     v( -1)-(INT)        -> 0001E2: 21                                                                                                      
            .Second                     : 56                     v( -1)-(INT)        -> 0001E1: 18                                                                                                      
        FirstRadioChannelUsed           : 00                     c( 12)-(INT)        -> 0001E4: 2F200100                                                                                                
        FirstAssignedSpeechCoderVersion : 2                      c( 61)-(INT)        -> 0001E8: 2F1D0102                                                                                                
        SpeechCoderPreferenceList       : 0201000501             c( 62)-(BYTE)       -> 0001EC: 2F1E050201000501                                                                                        
        CallingPartyNumber                                       c(  4)-(PARENT)     -> 0001F4: 8405                                                                                                    
          .TON                          : 4                      v( -1)-(MASK:41210000)-> 0001F6: 41                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 0001F6: 41                                                                                                    
          .Number                       : 27458525               v( -1)-(DIGIT)      -> 0001F7: 72545852                                                                                                
        BCSMTDPData01                                            c( 75)-(PARENT)     -> 0001FB: BF4B0E                                                                                                  
          .ServiceKey                   : 0000000D               c(  0)-(BYTE)       -> 0001FE: 80040000000D                                                                                            
          .gsmSCFAddress                                         c(  1)-(PARENT)     -> 000204: 8106                                                                                                    
            .TON                        : 1                      v( -1)-(MASK:41210000)-> 000206: 11                                                                                                    
            .NPI                        : 1                      v( -1)-(MASK:00004121)-> 000206: 11                                                                                                    
            .Number                     : 7774526205             v( -1)-(DIGIT)      -> 000207: 0524252650                                                                                              
        IncomingRoute                   : MK1B6CI                c( 21)-(ASCII)      -> 00020C: 254554511142164142                                                                                      
        OriginatedCode                  : 01                     c( 66)-(INT)        -> 000215: 2F420101                                                                                                
        OriginatingLineInformation      : 1E                     c(112)-(BYTE)       -> 000212: 2F810B011E                                                                                              
        CallIdentificationNumber        : 211402                 c(  1)-(BYTE)       -> 00021E: 8101211402                                                                                              
        NetworkCallReference            : 5A82640001             c( 68)-(BYTE)       -> 000221: 2F44055A82640001                                                                                        
        TypeOfCallingSubscriber         : 01                     c(  1)-(INT)        -> 00022B: 810101                                                                                                  
        TAC                             : 01020D                 c(  0)-(BYTE)       -> 00022E: 800101020D                                                                                              
        SubscriptionType                : 01                     c( 61)-(BYTE)       -> 000211: 2F1F0101                                                                                                
        OriginForCharging               : 00                     c( 16)-(BYTE)       -> 000217: 200100                                                                                                  
        RadioChannelProperty            : 01                     c( 58)-(INT)        -> 00021A: 2F1A0101                                                                                                
        ChargingCase                    : 0001                   c( 17)-(BYTE)       -> 00021E: 21020001                                                                                                
        ChargedParty                    : 00                     c( 15)-(INT)        -> 000242: 8F0100                                                                                                  
        TimeFromRegisterSeizureToStartOfCharging                 c( 14)-(PARENT)     -> 000245: 8E01                                                                                                    
          .Time                                                  u( -1)-(BYTE)                                                                                                                          
            .Hour                       : 00                     v( -1)-(INT)        -> 000247: 00                                                                                                      
            .Minute                     : 00                     v( -1)-(INT)        -> 000248: 00                                                                                                      
            .Second                     : 01                     v( -1)-(INT)        -> 000242: 01                                                                                                      
        InternalCauseAndLoc             : 0001                   c( 15)-(BYTE)       -> 00024A: 2F21020001                                                                                              
        CellIDForLastCellCalling                                 c( 28)-(PARENT)     -> 00024F: 2C07                                                                                                    
          .MCCMNC                       : 111 77                 v( -1)-(DIGIT)      -> 000251: 07F820                                                                                                  
          .MCC                          : 111                    i( -1)-(DETAIL)                                                                                                                        
          .MNC                          : 02                     i( -1)-(DETAIL)                                                                                                                        
          .LAC                          : 001A                   v( -1)-(BYTE)       -> 000254: 001A                                                                                                    
          .CI                           : 068B                   v( -1)-(BYTE)       -> 000256: 068B                                                                                                    
        EMLPPPriorityLevel              : 01                     c( 87)-(BYTE)       -> 000258: 2F570101                                                                                                
        IncommingAssignedRoute          : MK1B6DI                c( 21)-(ASCII)      -> 00025C: 2F5B0754511142164442                                                                                    
        TranslatedNumber                                         c( 74)-(PARENT)     -> 000266: 2F4A01                                                                                                  
          .TON                          : 4                      v( -1)-(MASK:41210000)-> 000262: 41                                                                                                    
          .NPI                          : 1                      v( -1)-(MASK:00004121)-> 000262: 41                                                                                                    
          .Number                       : C120                   v( -1)-(DIGIT)      -> 00026A: 1C02                                                                                                    
        OutgoingRoute                   : MK1SS1O                c( 22)-(ASCII)      -> 00026C: 26075451115151114F                                                                                      
        EosInfo                         : 00                     c( 14)-(BYTE)       -> 000275: 2F220100                                                                                                
        DisconnectingParty              : 00                     c(  8)-(INT)        -> 000272: 880100                                                                                                  
  *** Record is written

The output for this 2 blocks would be:
Code:
Type Of Record|ChargeableDuration|DateForStartOfCharge|ExchangeIdentity|RecordSequenceNumber|TimeForStartOfCharge|TimeForStopOfCharge|GSMCallReferenceNumber|MSCAddress|OutgoingRoute|CalledPartyNumber|NetworkCallReference|CallingPartyNumber|IncomingRoute
MSTerminating|00:01:11|20120111|ABRTTZ1_EMU57_J|123DED|10:12:57|10:14:01|464C6E0001|77745262512|MK1B7CO|77748710617|5A4A170001|22618102|MK1MK4I
MSOriginating,00:00:12,20120111,YYTTTZ1_EMU11_M,801AB0,10:11:57,10:14:02,5A82640001,77745262511,MK1SS1O,B20,5A82640001,27458525,MK1B6CI

I don't want you to do it for me, I want to understand your codes to be able to do it for myself, then I need to get the pattern in your logic to able to print more columns in the output. Please help me, thank you for all your help so far.

Regards
# 7  
Old 03-24-2012
You certainly had the hang of it. Because there are so many more variables than in the original sample I now used an extra array, for safety (otherwise original field values might get overwritten. Also in your second sample records now have preamble, for which I introduced a new test statement to discard them:

Code:
$1!="BeginOfRecord"{ next}

I made a few minor changes to the choice of field, it is only a matter of counting. Perhaps you can try and test this and make further adaptations to suit your need.....

Code:
awk '{$1=$1}1' infile |
awk '
BEGIN{
  header="Type Of Record|ChargeableDuration|DateForStartOfCharge|ExchangeIdentity|RecordSequenceNumber|TimeForStartOfCharge|TimeForStopOfCharge|GSMCallReferenceNumber|MSCAddress|OutgoingRoute|CalledPartyNumber|NetworkCallReference|CallingPartyNumber|IncomingRoute"
  print header
  m=split(header,H,"|")
}

$1!="BeginOfRecord"{ next}

{
  F[1]=$7
  for(i=8;i<=NF;i++) {
    if($i==H[2])  F[2]=$(i+11)":"$(i+19)":"$(i+27)
    if($i==H[3])  F[3]=$(i+8)
    if($i==H[4])  F[4]=$(i+2)
    if($i==H[5])  F[5]=$(i+2)
    if($i==H[6])  F[6]=$(i+11)":"$(i+19)":"$(i+27)
    if($i==H[7])  F[7]=$(i+11)":"$(i+19)":"$(i+27)
    if($i==H[8])  F[8]=$(i+2)
    if($i==H[9])  F[9]=$(i+22)
    if($i==H[10]) F[10]=$(i+2)
    if($i==H[11]) F[11]=$(i+22)
    if($i==H[12]) F[12]=$(i+2)
    if($i==H[13]) F[13]=$(i+22)
    if($i==H[14]) F[14]=$(i+2)
  }
  $0=x
  for(i=1;i<=m;i++){
    $i=F[i]
    delete F[i]
  }
}1
' RS= OFS=\|

The code uses RS= which separates records on empty line (two consecutive newline characters). The first small awk discards any spurious spaces and/or tab characters on those empty lines..

Last edited by Scrutinizer; 03-24-2012 at 05:41 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Blocks into table

please help, I have a huge file with blocks of data which I need to convert to a tabular format. Input sample id: GO:0000017 name: alpha-glucoside transport namespace: biological_process def: "The directed movement of alpha-glucosides into, out of or within a cell, or between... (3 Replies)
Discussion started by: ritakadm
3 Replies

2. Shell Programming and Scripting

Row blocks to column blocks

Hello, Searched for a while and found some "line-to-column" script. My case is similar but with multiple fields each row: S02 Length Per S02 7043 3.864 S02 54477 29.89 S02 104841 57.52 S03 Length Per S03 1150 0.835 S03 1321 0.96 S03 ... (9 Replies)
Discussion started by: yifangt
9 Replies

3. Shell Programming and Scripting

Transpose lines from individual blocks to unique lines

Hello to all, happy new year 2013! May somebody could help me, is about a very similar problem to the problem I've posted here where the member rdrtx1 and bipinajith helped me a lot. https://www.unix.com/shell-programming-scripting/211147-map-values-blocks-single-line-2.html It is very... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

4. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

5. UNIX for Dummies Questions & Answers

Convert 512-blocks to 4k blocks

I'm Unix. I'm looking at "df" on Unix now and below is an example. It's lists the filesystems out in 512-blocks, I need this in 4k blocks. Is there a way to do this in Unix or do I manually convert and how? So for container 1 there is 7,340,032 in size in 512-blocks. What would the 4k block be... (2 Replies)
Discussion started by: rockycj
2 Replies

6. Shell Programming and Scripting

Help with Script using Command Blocks

Hello, I am trying to create a shell script that use command block (donīt really know if this is the correct way to say it), but while one version works fine, the other one is not working at all. So let me show an example of this "command block" Iīm using and its working ok: cat << _EOF_ `echo... (7 Replies)
Discussion started by: Alexis Duarte
7 Replies

7. Shell Programming and Scripting

Removing blocks from a file

I have a file like the one below. Each record is separated with > In between I have lines consisting of 3 numeric values separated by a space. I need to take each block between the > sign and read the first number in the line. Then take the first after the > sign and the last before the >... (7 Replies)
Discussion started by: kristinu
7 Replies

8. Shell Programming and Scripting

How to read text in blocks

Hi, I have file which contains information written in blocks (every block is different). Is it possible to read every block one by one to another file (one block per file). The input is something like this <block1> <empty line> <block2> <empty line> ... ... ... <block25> <empty... (0 Replies)
Discussion started by: art84_)LV
0 Replies

9. Shell Programming and Scripting

Delete blocks with no data..

Hi, I tried this but could not get it... here is what I need I have an xml where I get all the data in blocks but some times I get empty blocks with no data...shown below..I need to delete only those blocks with no data, I tried couple of ways but could not do it..any help is appreciated...... (1 Reply)
Discussion started by: mgirinath
1 Replies
Login or Register to Ask a Question