根据两个不同的标签解析txt文件

根据两个不同的标签解析txt文件

我需要解析一个 txt 文件,如下所示。需要解析的部分以“SASN2010Aber.CallEventRecord.egsnPDPRecord”开头,文本位于两个括号之间{...}。关键文本长度不是恒定的,所以我无法根据位置解析它。如何解析和分离这些线组?

SASN2010Aber.CallEventRecord.egsnPDPRecord

{

    recordType : '70'D
    chargingID : '306457009'D
    sgsnAddress
    {
        Address : 'FBDC'H
    }
    pdpType : 'F121'H
    dynamicAddressFlag : '1'D
    listOfTrafficVolumes
    {
        [0]
        {
            changeTime : '1412031353342B0200'H
        }
    }
    duration : '0'D
    causeForRecClosing : '0'D
    recordSequenceNumber : '1'D
    rATType : '1'D
    listOfServiceData
    {
        [0]
        {
            ratingGroup : '4'D
            resultCode : '4010'D
            timeUsage : '0'D
            timeOfReport : '1412031353342B0200'H
            failureHandlingContinue : '0'D
            serviceIdentifier : '404'D
        }
    }

}

SASN2010Aber.CallEventRecord.egsnPDPRecord

{

    recordType : '70'D
    chargingID : '306457009'D
    sgsnAddress
    {
        Address : 'FBDC'H
    }
    pdpType : 'F121'H
    dynamicAddressFlag : '1'D
    listOfTrafficVolumes
    {
        [0]
        {
            changeTime : '1412031353342B0200'H
        }
    }
    causeForRecClosing : '0'D
    rATType : '1'D
    listOfServiceData
    {
        [0]
        {
            ratingGroup : '4'D
            resultCode : '4010'D
            failureHandlingContinue : '0'D
            serviceIdentifier : '404'D
        }
        [1]
        {
            ratingGroup : '4'D
            resultCode : '4010'D
            failureHandlingContinue : '0'D
            serviceIdentifier : '404'D
        }

    }
}

SASN2010Aber.CallEventRecord.egsnPDPRecord

{

    sgsnAddress
    {
        Address : 'FBDC'H
    }
    pdpType : 'F121'H
    listOfTrafficVolumes
    {
        [0]
        {
            changeTime : '1412031353342B0200'H
        }
    }
    duration : '0'D
    listOfServiceData
    {
        [3]
        {
            ratingGroup : '4'D
            resultCode : '4010'D
            timeUsage : '0'D
            serviceIdentifier : '404'D
        }
    }
}

答案1

有一个命令如下:

awk 'NR!=1{print RS$0 >"file"i++}' RS='SASN2010Aber.CallEventRecord.egsnPDPRecord' infile
  • 跳过NR!=1第一条空记录。
  • RS='...'定义SASN2010Aber.CallEventRecord.egsnPDPRecord埃科德S分离器
  • 并且该块print RS$0 >"file"i++将每个记录($0)保存到3个单独的文件中,其名称为file0file1以及file2(上面OP的示例输入中的3个文件)。
  • i++用于增加创建文件的数量。
  • infile将是您的输入文件的名称,以使用awk.

相关内容