0

I am trying to parse specific information from text files. I would like to be able to take the following text:

&15:44:25.3911456_0089& SIPEngine 0x208c  
N SIPUDPTransport::process_message() : remote=10.250.2.18:5060, message=
To: sip:%QBE-JV9SKV1-SP%@10.250.2.18:5060;tag=6642

&15:44:31.7504799_0013& SIPEngine 0x2014  
N SIPUDPTransport::process_message() : remote=10.250.2.85:5060, message=
To: sip:%QBE-75FHBW1-SP%@10.250.2.85:5060;tag=11225

And parse it into the following data:

15:44:25.3911456_0089,QBE-JV9SKV1-SP
15:44:31.7504799_0013,QBE-75FHBW1-SP

The & and % were added to be there as delimiters. I can get creative with find and replace to add delimiters like this.

Any help on how to parse the data like this would be greatly appreciated. I have been trying to use SED, FOR /F, and other utilities but I haven't had much luck yet. The pattern of information will always repeat one after the other.

Essentially I am just trying to identify delimited strings within a text file and output them so I hope that this is something that can help others as well.

Thanks in advance for any help!

Endoro
  • 37,015
  • 8
  • 50
  • 63

1 Answers1

1

Try this:

@echo off&setlocal
(for /f "tokens=1,2delims=&%%" %%i in ('findstr "SIPEngine QBE" file.txt') do (
    set "line1=%%i"
    set "line2=%%j"
    setlocal enabledelayedexpansion
    if "!line2:SIPEngine=!" neq "!line2!" (<nul set/p"=!line1!,"
    ) else if "!line2:QBE=!" neq "!line2!" echo(!line2!
    endlocal
))>out.txt
type out.txt

..output is:

15:44:25.3911456_0089,QBE-JV9SKV1-SP
15:44:31.7504799_0013,QBE-75FHBW1-SP
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Hello @Endoro, Thanks for the help and this looks like it will get me down the right path. How are you running this? I tried to put this into a batch file and run it but it does not terminate. – Lincoln Rose May 17 '13 at 22:34
  • 2
    @LincolnRose - FINDSTR hangs on XP and Windows 7 if redirected input does not end with `` (linefeed). See [What are the undocumented features and limitations of the Windows FINDSTR command?](http://stackoverflow.com/q/8844868/1012053) for more information. Remove the redirection and use `('findstr "SIPEngine QBE" file.txt')` instead, and it should work. – dbenham May 18 '13 at 00:16