Search by substring and extract string

Home Forums Scripting PowerShell Search by substring and extract string

This topic contains 3 replies, has 3 voices, and was last updated by  huynhgiang 7 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts

  • ckiani7
    Member
    #167535

    I am trying to extract a string from a text file by matching with a substring.

    I cannot get my script to extract the whole line (String) when it finds the particular substring, all it does is output the substring found. Has anyone got any ideas on how this can be done.

    The text file is huge and is a list of dns queries all I want is the string with a particular hostname and IP address and then output this to another text file.

    Thanks in advance. :bowdown:


    Ossian
    Moderator
    #191993

    Could you give us a few lines from the file, and also the substring, as an example – it is a bit difficult to visualise. PowerShell being PowerShell, I am sure there is a way

    Also can you give the script you are using at the moment (that doesn’t quite work)


    huynhgiang
    Member
    #392022

    Here is a snippet of the text file:

    03/05/2018 15:25:11 0A00 PACKET 0000000D9E9A8120 UDP Rcv 10.100.82.45 08c2 Q [0001 D NOERROR] A (9)localhost(0)

    03/05/2018 15:25:11 0A04 PACKET 0000000D9F97E150 UDP Rcv 10.100.82.45 093b Q [0001 D NOERROR] AAAA (9)localhost(0)

    03/05/2018 15:25:11 0A00 PACKET 0000000DA0426150 UDP Rcv 10.100.82.45 d652 Q [0001 D NOERROR] AAAA (4)env6(10)

    03/05/2018 15:25:11 0A04 PACKET 0000000D9D3580A0 UDP Rcv 10.100.82.45 b9f5 Q [0001 D NOERROR] A (4)env6(10)

    03/05/2018 15:25:11 0A04 PACKET 0000000D9FE84240 UDP Rcv 10.100.1.81 5906 Q [0001 D NOERROR] A (5)axtel(3)net(8)c22fc498(3)ubl(5)dnsbl(10)borderware(3)com(0)

    I am trying to extract every entry that has the substring “axtel” in it for debugging purposes and unfortunately I gave up and deleted the script I was using. I was using the Select-String cmdlet along with the Pattern parameter. One issue is that the text file is being updated regularly and the entries will be random where they appear.


    Ossian
    Moderator
    #191994

    Thanks – it make it clearer
    I suspect the “select-substring” is looking inside the file as a whole, but if you use Get-Content, you can process it line by line
    There are some examples here: https://stackoverflow.com/questions/33511772/read-file-line-by-line-in-powershell which may help you – look line by line and then use a regex to match “axtel”, then divert the line into another file or whatever you want to do with it

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.