Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2014
    Posts
    1

    Unanswered: How to write a script to use grep to extract all specific parts of in a log file

    Dear Friends:

    I have a file as attached,

    I hope to write a script to extract all specific parts of in a file



    it requires:


    [1] that part start with TransactionID like:
    EtransactionID10000001
    [2]. then after 1 space line, in 3rd line, it must contain :
    <ns0:WebServiceRequest>

    [3]. then at the end, this part must contain :
    </ns0:WebServiceRequest>

    so in this case each part that has EtransactionID10000001 at 1st line and <ns0:WebServiceRequest> at 3rd line and <ns0:WebServiceRequest> at last line will be extracted and printed out;
    but parts of containing TransactionID like EtransactionID10000022 and EtransactionID10000033 will not be selected


    Please advice How to write a script to use grep or smth to extract all specific parts of in a file??

    Thanks a lot!!

    sunny

    or see my file below:


    2014-08-21 17:10:28,489 test123
    111 | EtransactionID10000001 | call webService=> start


    2014-08-21 17:10:28,489 test123
    112 | EtransactionID10000001 | call webService=> invoke service


    2014-08-21 17:10:28,489 test123
    113 | EtransactionID10000001 | call webService=> send Request

    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:WebServiceRequest>
    <ns0:MessageHeader>
    <ns2:dateTimeStamp>2014-03-31T03:47:46Z</ns2:dateTimeStamp>
    </ns1:TrackingMessageHeader>
    </ns0:MessageHeader>
    <ns0:WebServiceRequest>
    <ns1:Request>
    <ns1:Employee>
    <ns1:firstName>AAA</ns1:firstName>
    <ns1:lastName>BBB</ns1:lastName>
    </ns1:Employee>
    </ns1:Request>
    </ns0:WebServiceRequest>
    </ns0endAccountNotificationRequest>


    2014-08-21 17:10:28,489 test123
    114 | EtransactionID10000022 | call webService=> send Request

    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:WebServiceRequest>
    <ns0:MessageHeader>
    <ns2:dateTimeStamp>2014-03-31T03:47:46Z</ns2:dateTimeStamp>
    </ns1:TrackingMessageHeader>
    </ns0:MessageHeader>
    <ns0:WebServiceRequest>
    <ns1:Request>
    <ns1:Employee>
    <ns1:firstName>AAA</ns1:firstName>
    <ns1:lastName>BBB</ns1:lastName>
    </ns1:Employee>
    </ns1:Request>
    </ns0:WebServiceRequest>
    </ns0endAccountNotificationRequest>


    2014-08-21 17:10:28,489 test123
    115 | EtransactionID10000033 | call webService=> send Request

    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:WebServiceRequest>
    <ns0:MessageHeader>
    <ns2:dateTimeStamp>2014-03-31T03:47:46Z</ns2:dateTimeStamp>
    </ns1:TrackingMessageHeader>
    </ns0:MessageHeader>
    <ns0:WebServiceRequest>
    <ns1:Request>
    <ns1:Employee>
    <ns1:firstName>AAA</ns1:firstName>
    <ns1:lastName>BBB</ns1:lastName>
    </ns1:Employee>
    </ns1:Request>
    </ns0:WebServiceRequest>
    </ns0:WebServiceRequest>

    2014-08-21 17:10:28,489 test123
    116 | EtransactionID10000001 | call webService=> send Request

    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:WebServiceRequest>
    <ns0:MessageHeader>
    <ns2:dateTimeStamp>2014-03-31T03:47:46Z</ns2:dateTimeStamp>
    </ns1:TrackingMessageHeader>
    </ns0:MessageHeader>
    <ns0:WebServiceRequest>
    <ns1:Request>
    <ns1:Employee>
    <ns1:firstName>AAA</ns1:firstName>
    <ns1:lastName>BBB</ns1:lastName>
    </ns1:Employee>
    </ns1:Request>
    </ns0:WebServiceRequest>
    </ns0:WebServiceRequest>
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    You need to use either awk or perl, grep alone will not do it.

    PS: Post expected results based on the source you provided.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    The following code will extract only the xml code that you are interested in
    If you use bash, you can change the 'cut' statements to sub strings
    Once you are convinced that only the required lines are in the output, then you can comment the echo $prevcode|a line and uncomment the echo $a line. Where t1 is the input file.

    Code:
    IFS="|"                                                  
    pprint="n"                                               
    while read a b c                                         
    do                                                       
            d=`echo $a |cut -c1`                             
            if [ "$d" = '1' ]                                
            then                                             
                    if [ "$b" = " EtransactionID10000001 " ] 
                    then                                     
                    prevcode=$b                              
                    pprint="y"                               
                    else                                     
                    pprint="n"                               
                    fi                                       
            fi                                               
            if [ "$d" = "<"  -a $pprint = 'y' ]              
            then                                             
                    echo $prevcode "|" $a    
                 #   echo $a                
            fi                                               
    done <t1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •