0

I have a continuously changing text file that contains messages from the Gmail API. I need to parse the contents of it. My text file contains the id of the message, the message, sender details and the date:

15335b114b093448(message id)::NAME : SINDHUJA, 
SERVICE REQUIRED: PLUMBER ,CITY :BANGALORE, EMAIL ID : [email protected] , MOBILE : 9987654434::  Wed, 02 Mar 2016 04:58:29 +0000::Flipkart(partner) [email protected]:::
15335254a7e5677a::NAME : HALEN, SERVICE REQUIRED: ELECTRICIAN ,CITY :CHENNAI, EMAIL ID : [email protected] ,MOBILE : 934554434:: ::Wed, 2 Mar 2016 07:36:37 +0530 (IST)::The Times of India [email protected]::::

How do I parse it? I tried the code:

 >>>rawProfiles='''
    15335b114b093448::NAME : SINDHUJA, 
SERVICE REQUIRED: PLUMBER ,CITY :BANGALORE, EMAIL ID : [email protected] ,MOBILE : 9987654434::  Wed, 02 Mar 2016 04:58:29 +0000::Flipkart <[email protected]>:::
    15335254a7e5677a::NAME : HALEN, 
SERVICE REQUIRED: ELECTRICIAN ,CITY :CHENNAI, EMAIL ID : [email protected] ,MOBILE : 934554434:: ::Wed, 2 Mar 2016 07:36:37 +0530 (IST)::The Times of India <[email protected]>::::
    '''
    >>>profilesList=re.split(r'\n{1,}',rawProfiles)

    >>>profilesList =[re.sub(r'\n',' ',profile) for profile in profilesList]

    >>>profilesList =[re.split(r',',profile,maxsplit=2) for profile in profilesList]
    >>>for profile in profilesList:
    print profile

    >>>profilesList =[map(str.strip,profile) for profile in profilesList]
    >>>for profile in profilesList:
    print profile

This gave me a: nameError re not defined.

EDIT

The code is in python.

My sample output should be :

 PARTNER       NAME       SERVICE     CITY        MOBILE     EMAIL  
Flipkart SINDHUJA PLUMBER BANGALORE 9987654434 [email protected]
Times HALEN ELECTRICIAN CHENNAI 9345544342 [email protected]

7
  • 1
    Please edit your question to include example output. And just out of interest, what language is that code?
    – jasonwryan
    Mar 2, 2016 at 5:55
  • Sorry but your post is very hard to read. Please use the formatting tools to make your text appear like code. We need to see exactly what the actual ionput data look like.
    – terdon
    Mar 2, 2016 at 8:33
  • @terdon I have edited it...!
    – Indhuja
    Mar 2, 2016 at 12:11
  • Please use the formatting tools as I asked and not HTML markup. Do you really want empty lines between each line of output? Do you really need the headers in bold? We need to see exactly what you want.
    – terdon
    Mar 2, 2016 at 12:27
  • @terdon i jus need the fields of every message separately ... how do i do it ? And the message should be parsed without being case sensitive..
    – Indhuja
    Mar 2, 2016 at 13:57

1 Answer 1

2

There is a small problem with the Partner column of the second line, but this awk can serve you as a beginning.

BEGIN {
    FS="::";
    OFS="\t";
    print "PARTNER\tNAME\tSERVICE\tCITY\tMOBILE\tEMAIL"
}
{
    split($2,A,"[:,]");
    if ($3 == " ")
    {
        split($5,B,"[:,]");
    }
    else
    {
        split($4,B,"[:,]");
    }

    split(B[1],C,"mail");
    name=A[2];
    service=A[4];
    city=A[6];
    mobile=A[10];
    email=A[8];
    partner=C[1];
    sub("[(]partner[)]", "",partner);

    print partner"\t"name"\t"service"\t"city"\t"mobile"\t"email;
}

Out:

PARTNER NAME    SERVICE CITY    MOBILE  EMAIL
Flipkart     SINDHUJA    PLUMBER    BANGALORE    9987654434  [email protected] 
The Times of India   HALEN   ELECTRICIAN    CHENNAI  934554434   [email protected] 

You have to run it from the (bash,zsh..)shell and save it as whatever.awk

awk -f whatever.awk file

10
  • I am a newbie to awk...Where do i run this code ? in the python shell ? Thank you for the efforts !
    – Indhuja
    Mar 2, 2016 at 13:59
  • @Indhuja I just edit it.
    – Firefly
    Mar 2, 2016 at 14:16
  • 1
    For most file types, with some exceptions, awk is best to process text. And, it is installed by default in unix and now in linux.
    – Firefly
    Mar 2, 2016 at 14:25
  • Thank you !!! where do i mention the name of the text file to parse? @Firefly
    – Indhuja
    Mar 2, 2016 at 14:27
  • awk -f script.awk FileToParse
    – Firefly
    Mar 2, 2016 at 14:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .