Log processing with regerx and slice





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have a log partner as below i just want to be separate it those are in between bracket including time from the beginning. have tried below code as well but no luck .



def crop_string(line):
try:
#str0 = line.replace(']',"")
#str1 = str0.replace('.000+05:30',"")
str2 = line.split()
#str2 = list(dict.fromkeys(str1))
#x = len(str2)
print(str2[0],str2[5])
pass
except IndexError:
print("exception",str2)
pass


log



"INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO   :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]"









share|improve this question























  • What does "no luck" mean? If this does not work, what happens instead? Do you get an error? (Please do not answer in a comment. Use edit to add all missing information to your question.)

    – usr2564301
    Nov 25 '18 at 10:08


















-1















I have a log partner as below i just want to be separate it those are in between bracket including time from the beginning. have tried below code as well but no luck .



def crop_string(line):
try:
#str0 = line.replace(']',"")
#str1 = str0.replace('.000+05:30',"")
str2 = line.split()
#str2 = list(dict.fromkeys(str1))
#x = len(str2)
print(str2[0],str2[5])
pass
except IndexError:
print("exception",str2)
pass


log



"INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO   :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]"









share|improve this question























  • What does "no luck" mean? If this does not work, what happens instead? Do you get an error? (Please do not answer in a comment. Use edit to add all missing information to your question.)

    – usr2564301
    Nov 25 '18 at 10:08














-1












-1








-1








I have a log partner as below i just want to be separate it those are in between bracket including time from the beginning. have tried below code as well but no luck .



def crop_string(line):
try:
#str0 = line.replace(']',"")
#str1 = str0.replace('.000+05:30',"")
str2 = line.split()
#str2 = list(dict.fromkeys(str1))
#x = len(str2)
print(str2[0],str2[5])
pass
except IndexError:
print("exception",str2)
pass


log



"INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO   :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]"









share|improve this question














I have a log partner as below i just want to be separate it those are in between bracket including time from the beginning. have tried below code as well but no luck .



def crop_string(line):
try:
#str0 = line.replace(']',"")
#str1 = str0.replace('.000+05:30',"")
str2 = line.split()
#str2 = list(dict.fromkeys(str1))
#x = len(str2)
print(str2[0],str2[5])
pass
except IndexError:
print("exception",str2)
pass


log



"INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO   :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]"






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 7:13









Sunil SahooSunil Sahoo

1015




1015













  • What does "no luck" mean? If this does not work, what happens instead? Do you get an error? (Please do not answer in a comment. Use edit to add all missing information to your question.)

    – usr2564301
    Nov 25 '18 at 10:08



















  • What does "no luck" mean? If this does not work, what happens instead? Do you get an error? (Please do not answer in a comment. Use edit to add all missing information to your question.)

    – usr2564301
    Nov 25 '18 at 10:08

















What does "no luck" mean? If this does not work, what happens instead? Do you get an error? (Please do not answer in a comment. Use edit to add all missing information to your question.)

– usr2564301
Nov 25 '18 at 10:08





What does "no luck" mean? If this does not work, what happens instead? Do you get an error? (Please do not answer in a comment. Use edit to add all missing information to your question.)

– usr2564301
Nov 25 '18 at 10:08












1 Answer
1






active

oldest

votes


















0














re.findall should do the trick. Check the code snippet below to see, how it could be used with non-greedy regular expressions.
Still, I advise you avoiding such simple solutions: string data containing brackets can lead to incorrect results.



# -*- coding: utf-8 -*-

from __future__ import print_function
import re


def crop_string(line):
log_time = re.search(r"d{4}-d{2}-d{2}Td{2}:d{2}:d{2}", line)
if log_time is None:
raise Exception("The line doesn't include a timestamp")
log_brackets_list = re.findall("[(.*?)]", line)
return log_time.group(0), log_brackets_list


if __name__ == '__main__':

time, brackets_list = crop_string("""INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]""")
print(time, brackets_list)





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465428%2flog-processing-with-regerx-and-slice%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    re.findall should do the trick. Check the code snippet below to see, how it could be used with non-greedy regular expressions.
    Still, I advise you avoiding such simple solutions: string data containing brackets can lead to incorrect results.



    # -*- coding: utf-8 -*-

    from __future__ import print_function
    import re


    def crop_string(line):
    log_time = re.search(r"d{4}-d{2}-d{2}Td{2}:d{2}:d{2}", line)
    if log_time is None:
    raise Exception("The line doesn't include a timestamp")
    log_brackets_list = re.findall("[(.*?)]", line)
    return log_time.group(0), log_brackets_list


    if __name__ == '__main__':

    time, brackets_list = crop_string("""INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]""")
    print(time, brackets_list)





    share|improve this answer




























      0














      re.findall should do the trick. Check the code snippet below to see, how it could be used with non-greedy regular expressions.
      Still, I advise you avoiding such simple solutions: string data containing brackets can lead to incorrect results.



      # -*- coding: utf-8 -*-

      from __future__ import print_function
      import re


      def crop_string(line):
      log_time = re.search(r"d{4}-d{2}-d{2}Td{2}:d{2}:d{2}", line)
      if log_time is None:
      raise Exception("The line doesn't include a timestamp")
      log_brackets_list = re.findall("[(.*?)]", line)
      return log_time.group(0), log_brackets_list


      if __name__ == '__main__':

      time, brackets_list = crop_string("""INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]""")
      print(time, brackets_list)





      share|improve this answer


























        0












        0








        0







        re.findall should do the trick. Check the code snippet below to see, how it could be used with non-greedy regular expressions.
        Still, I advise you avoiding such simple solutions: string data containing brackets can lead to incorrect results.



        # -*- coding: utf-8 -*-

        from __future__ import print_function
        import re


        def crop_string(line):
        log_time = re.search(r"d{4}-d{2}-d{2}Td{2}:d{2}:d{2}", line)
        if log_time is None:
        raise Exception("The line doesn't include a timestamp")
        log_brackets_list = re.findall("[(.*?)]", line)
        return log_time.group(0), log_brackets_list


        if __name__ == '__main__':

        time, brackets_list = crop_string("""INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]""")
        print(time, brackets_list)





        share|improve this answer













        re.findall should do the trick. Check the code snippet below to see, how it could be used with non-greedy regular expressions.
        Still, I advise you avoiding such simple solutions: string data containing brackets can lead to incorrect results.



        # -*- coding: utf-8 -*-

        from __future__ import print_function
        import re


        def crop_string(line):
        log_time = re.search(r"d{4}-d{2}-d{2}Td{2}:d{2}:d{2}", line)
        if log_time is None:
        raise Exception("The line doesn't include a timestamp")
        log_brackets_list = re.findall("[(.*?)]", line)
        return log_time.group(0), log_brackets_list


        if __name__ == '__main__':

        time, brackets_list = crop_string("""INFO:DEL:2018-11-24T14:04:49.000+05:30 bltxn03 ChannelGatewayRequestLog INFO :: [ReqOut:][RQID:1958259][STV:1RC][S:EXTGW][RQC:17007][UN:banti mobile centre ][CAT:RET][MSISDN:334455668][USt:Y][UNW:UW][DSMS:1RC 44xxxxxx 11111.0 VG ****][TEMPTID:null][UDH:313231][ST:EXTGW][SRVPRT:190][OINFO:RETAPPUW,MsgReq=true,FT=R ResTyp=RESPONSE][RETMSG:<?xml version="1.0"?><!DOCTYPE COMMAND PUBLIC "-//Ocam//DTD XML Command 1.0//EN" "xml/command.dtd"><COMMAND><TYPE>EXRC121TRFRESP</TYPE><TXNSTATUS>17007</TXNSTATUS><DATE>4/10/2018 14:04:49</DATE><EXTREFNUM>192108828_1548487626</EXTREFNUM><TXNID>UW445566126</TXNID><MESSAGE>Your request cannot be processed at this time, please try again later.</MESSAGE></COMMAND>][FixdInfNtAvail.][TT:860 ms]""")
        print(time, brackets_list)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 9:07









        reartnewreartnew

        1978




        1978
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465428%2flog-processing-with-regerx-and-slice%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud