Parsing a txt file according to two different tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a txt file according to two different tags
# 1  
Old 02-23-2015
Parsing a txt file according to two different tags

I need to parse a txt file like below.

starts from the first occurence of
SASN2010Aber.CallEventRecord.egsnPDPRecord
{

till the last occurence of
}


in other saying name of the part(header) is SASN2010Aber.CallEventRecord.egsnPDPRecord and the content of the header is in two parenthesis {}, untill the next header.

How can I parse and separate these headers including content?

text file is below;
Code:
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
        }
    }
}

the output need to be like below (3 divided);
Code:
SASN2010Aber.CallEventRecord.egsnPDPRecord
{
.
.
.
.
.
.
}

Code:
SASN2010Aber.CallEventRecord.egsnPDPRecord
{
.
.
.
.
.
.
}

Code:
SASN2010Aber.CallEventRecord.egsnPDPRecord
{
.
.
.
.
.
.
}


Last edited by snr_silencer; 02-23-2015 at 09:20 AM.. Reason: Added ICODE tags
# 2  
Old 02-23-2015
Any attempts from your side?

There are three separated records with that name in your sample file. So - what exactly do you want to achieve?

---------- Post updated at 14:19 ---------- Previous update was at 14:17 ----------

And, what's the "two different tags" in the thread header?
# 3  
Old 02-23-2015
If you want to parse matching {} you will need a parsing algorithm using a stack. On the other hand if you rely on nice formatting so you just want to extract stuff between curly braces found in the first column things are easier. The following perl script for example will split the input into several output files:

Code:
#!/usr/bin/perl

my $nr=0;
my $fh=undef;

while (<>) 
{ if ( m/^{/ )
  { open($fh, ">", "output" . $nr) or die "Can't open output file !";
  }
  elsif ( m/^}/ )
  { close($fh);
    $fh = undef;
    $nr++;
  }
  elsif ($fh)
  { print $fh $_;
  }
}

# 4  
Old 02-23-2015
I know title is little bit wrong. I thought one was
Code:
SASN2010Aber.CallEventRecord.egsnPDPRecord
{

other one was
Code:
}

Actually there is just one and it is
Code:
SASN2010Aber.CallEventRecord.egsnPDPRecord

---------- Post updated at 10:11 AM ---------- Previous update was at 09:33 AM ----------

@Walter, thanks for your help. It works great.
There just two minor detail.

1- Can it keep the original format in the output files (I mean wiht header and parenthesis)
Code:
SASN2010Aber.CallEventRecord.egsnPDPRecord
{
.
.
.
.
.
.
}

2- Can it seperate if input includes a specific line like
Code:
ratingGroup : '4'D

?

thanks a lot. Your perl script will help me a lot even with this version.

---------- Post updated at 11:17 AM ---------- Previous update was at 10:11 AM ----------

ok, it's done in another way
thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

2. Windows & DOS: Issues & Discussions

Parsing a UNIX txt to separate files

I have a requirement to parse a dataflex .txt file and break it into separate files within the Windows server env. Is there any special characters I should pay particular attention on the unix side? Any ideas? Thanks in advance (2 Replies)
Discussion started by: kicklinr
2 Replies

3. Shell Programming and Scripting

XML Parsing having optional tags into flat file

In xml file i have following data where some tags like<ChrgBr> may not be present in every next file. So i want these values to be stored in some variable like var1="405360,00" , var2="DEBT" and so on ,but if <ChrgBr> tag has no value or is absent var2 should have space like var2=" " so that i... (1 Reply)
Discussion started by: sandipgawale
1 Replies

4. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. Shell Programming and Scripting

Parsing txt, xml files and preparing csv file

Hi, I need to parse text, xml files to get the statistic numbers and prepare summary csv file. What is the best way to parse these file and prepare csv file. Any idea you have , please? Regards, (2 Replies)
Discussion started by: LinuxLearner
2 Replies

7. Shell Programming and Scripting

parsing txt file, saving graphics files

hi everyone, i am a newbie in shell programming. and i want to simply go through a text file that contains 3 "columns", split by ';' customerID ; link-to-contract ; save-as-filename so an example would simply look like this now i want to loop through every line, and save the file from... (3 Replies)
Discussion started by: Confidence
3 Replies

8. Shell Programming and Scripting

help parsing txt with awk

Hi all I would need some help to introduce 'break' lines into the following data by using awk: original data: RECORD 1000 aaa xxxxxx RECORD 1001 aaa xxxxxx RECORD 1002 bbb xxxxxx RECORD 1003 bbb xxxxxx RECORD 1004 bbb xxxxxx RECORD 1005 ccc xxxxxx RECORD 1006 ccc xxxxxx RECORD 1007... (5 Replies)
Discussion started by: yomaya
5 Replies

9. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies

10. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies
Login or Register to Ask a Question