Regex insertion

Discussion in 'Perl Misc' started by sil, Jul 18, 2007.

  1. sil

    sil Guest

    I need a regex of sorts that will replace ONLY the line: P-Asserted-
    Identity: <sip:+$fU@$tU;user=phone> with:
    P-Asserted-Identity: <sip:19998887777@1.2.3.4:5060> (from line 1)

    Here is my data:

    f:"Operations"<sip:
    19998887777@1.2.3.4:5060>;tag=3933ee6e1923de3601d9dab88b1dc4bf
    t:<sip:>
    m:<sip:0-d03365c213c46d247564@6.7.8.9:5060;transport=udp>
    i:bf52f59d069087948090769a8a98fcdd-469d50d7@1.2.3.4
    CSeq:21796 INVITE
    User-Agent: Asterisk PBX
    Max-Forwards:16
    Date: Tue, 17 Jul 2007 23:29:27 GMT
    Allow:INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY
    c:application/sdp
    l:289
    Remote-Party-ID: <sip:+$fU@$tU>;privacy=off;screen=no
    P-Asserted-Identity: <sip:+$fU@$tU;user=phone>

    1 will always start with "f:"

    Maybe...
    subst("/From: <sip.*)(@)(.*)/From: <sip:\1\2\3\r\nP-Asserted-
    Identity: ?

    Is there an alternative say if P-Asserted... was ALWAYS the 13th line

    #!/usr/bin/perl

    my $sipPacket = 1;
    my @packetData = '';

    while ( my $line = <> ) {

    if ( $line =~ m/^From: <sip.*/ ) {

    if ( $sipPacket < 1 ){
    pwnPacket(@packetData);
    @packetData = '';
    }

    $sipPacket--;
    next;
    }

    push @packetData, $line;

    }

    sub pwnPacket {
    if ($_[1] =~ /From: <sip.*)(@)(.*)/) {

    # Confusion seeps in... How do I insert it into the 13 field...
    print "From: <sip:\1\2\3\r\nP-Asserted-
    Identity:";


    }

    So the packet would read:
    f:"Operations"<sip:
    19998887777@1.2.3.4:5060>;tag=3933ee6e1923de3601d9dab88b1dc4bf
    t:<sip:>
    m:<sip:0-d03365c213c46d247564@10.101.1.20:5060;transport=udp>
    i:bf52f59d069087948090769a8a98fcdd-469d50d7@1.2.3.4
    CSeq:21796 INVITE
    User-Agent: Asterisk PBX
    Max-Forwards:16
    Date: Tue, 17 Jul 2007 23:29:27 GMT
    Allow:INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY
    c:application/sdp
    l:289
    Remote-Party-ID: <sip:+$fU@$tU>;privacy=off;screen=no
    P-Asserted-Identity: <sip:19998887777@1.2.3.4:5060>
    sil, Jul 18, 2007
    #1
    1. Advertising

  2. sil <> wrote:
    > I need a regex of sorts that will replace ONLY the line: P-Asserted-
    > Identity: <sip:+$fU@$tU;user=phone> with:
    > P-Asserted-Identity: <sip:19998887777@1.2.3.4:5060> (from line 1)


    > Here is my data:


    > f:"Operations"<sip:
    > 19998887777@1.2.3.4:5060>;tag=3933ee6e1923de3601d9dab88b1dc4bf
    > t:<sip:>
    > m:<sip:0-d03365c213c46d247564@6.7.8.9:5060;transport=udp>
    > i:bf52f59d069087948090769a8a98fcdd-469d50d7@1.2.3.4
    > CSeq:21796 INVITE
    > User-Agent: Asterisk PBX
    > Max-Forwards:16
    > Date: Tue, 17 Jul 2007 23:29:27 GMT
    > Allow:INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY
    > c:application/sdp
    > l:289
    > Remote-Party-ID: <sip:+$fU@$tU>;privacy=off;screen=no
    > P-Asserted-Identity: <sip:+$fU@$tU;user=phone>


    > 1 will always start with "f:"


    > So the packet would read:
    > f:"Operations"<sip:
    > 19998887777@1.2.3.4:5060>;tag=3933ee6e1923de3601d9dab88b1dc4bf
    > t:<sip:>
    > m:<sip:0-d03365c213c46d247564@10.101.1.20:5060;transport=udp>
    > i:bf52f59d069087948090769a8a98fcdd-469d50d7@1.2.3.4
    > CSeq:21796 INVITE
    > User-Agent: Asterisk PBX
    > Max-Forwards:16
    > Date: Tue, 17 Jul 2007 23:29:27 GMT
    > Allow:INVITE,ACK,CANCEL,OPTIONS,BYE,REFER,SUBSCRIBE,NOTIFY
    > c:application/sdp
    > l:289
    > Remote-Party-ID: <sip:+$fU@$tU>;privacy=off;screen=no
    > P-Asserted-Identity: <sip:19998887777@1.2.3.4:5060>


    What about something (assuming that the line break in the line
    starting with 'f:' is the result of mangling by my newsreader)
    as simple as

    my $f;
    while ( my $line = <> ) {
    $f = $1 if $line =~ /^f:[^<]*<(sip:[^>]*)>;/;
    $line =~ s/sip:[^>]*/$f/ if $line =~ /^P-Asserted-Identity:/;
    print $line;
    }
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 18, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rajan

    Insertion delay

    rajan, Sep 17, 2004, in forum: VHDL
    Replies:
    1
    Views:
    3,566
    Mike Treseler
    Sep 18, 2004
  2. Boban Dragojlovic

    automatic insertion of controls on codebehind

    Boban Dragojlovic, Jul 23, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    404
    Boban Dragojlovic
    Jul 24, 2003
  3. Replies:
    2
    Views:
    485
    Jerry III
    Aug 11, 2003
  4. Replies:
    3
    Views:
    754
    Reedick, Andrew
    Jul 1, 2008
  5. sil

    Regex insertion

    sil, Jul 18, 2007, in forum: Perl Misc
    Replies:
    0
    Views:
    89
Loading...

Share This Page