Wildcard text search

Welcome Forums General PowerShell Q&A Wildcard text search

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5541

    Points: 0
    Rank: Member

    by Sum4uandSum4u at 2013-02-13 07:30:22

    I have a text file that contains this line.
    Default Gateway . . . . . . . . . :

    I want to be able to search for anything in the ip space and grab that info and place it in a csv file. I need to create a search string that will pick up anything in the ip section of the line, wheher it is or

    get-childitem "c:\test" -include *.txt -recurse |
    select-string -pattern "Default Gateway . . . . . . . . . : I want to place something in here ???.???.???.???" | Select-Object filename,pattern |Export-csv c:\test\IP.csv

    by DonJ at 2013-02-13 07:43:57

    So, you're after a regex.


    Will match an IP address, although it will match anything that looks like one – including 327.789.234.555. It's just looking for patterns of 3 digits and periods.

    by mjolinor at 2013-02-13 07:49:56

    A generic regex to match an IPV4 address would look like:


    But select-string is going to capture the entire line, and returns a collection of matchinfo objects.
    The matchinfo objects contain a lot of (potentially) useful information, but can be rather clumsy to work with if you're just needing to match and extract substrings.

    Personally I prefer to use a chained -match -replace for this kind of work:

    $regex = 'Default Gateway [. :]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'

    get-childitem "c:\test" -include *.txt -recurse |

    foreach {

    (get-content $_.fullname -ReadCount 0) -match '^Default Gateway' -replace $regex,'$1'


    by Sum4uandSum4u at 2013-02-13 08:33:32

    Thanks for the responses! mjolinor, how do i get the results exported to a csv file? Thanks in advance.

    by mjolinor at 2013-02-13 08:54:01

    Are you sure it should be a .csv file ? A .csv file with just one column of data is basically a list. I'd just export it as a .txt file.

    $regex = 'Default Gateway [. :]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'

    get-childitem "c:\test" -include *.txt -recurse |

    foreach {

    (get-content $_.fullname -ReadCount 0) -match '^Default Gateway' -replace $regex,'$1'

    } | set-content c]

    If you really want a .csv file you can replace set-content with:

    foreach {
    New-Object psobject -Property @{Gateway=$_}
    } | Export-Csv C:\somedir\gateways.csv -NoTypeInformation

    But the only difference will be that it will have "Gatway" as the first line of the file (as the csv header).

    by Sum4uandSum4u at 2013-02-13 08:56:36

    There is a second entry in the csv file. I am taking the name of the text file and placing it in the spread sheet as well.

    by mjolinor at 2013-02-13 09:26:25

    That changes things. Is there only one gateway address line per file?

    by Sum4uandSum4u at 2013-02-13 09:35:34

    Thanks in advance mjolinor. Please advise

    When I run the code below, the only thing in the csv file is the name of the file with no reference to the gateway line.
    If I attempt to run the text config above, the file is never created 🙁

    $regex = 'Default Gateway [. :]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
    get-childitem "c:\test" -include *.txt -recurse |

    foreach {

    (get-content $_.fullname -ReadCount 0) -match '^Default Gateway' -replace $regex,'$1'
    New-Object psobject -Property @{Gateway=$_} } | Export-Csv C:\test\gateways.csv -NoTypeInformation

    by mjolinor at 2013-02-13 11:09:22

    Try this:
    $regex = 'Default Gateway [. :]+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'

    $files =
    get-childitem "c:\test" -include *.txt -recurse

    foreach ($file in $files)
    $filename = $file.FullName

    $gateway =
    (get-content $filename -ReadCount 0) -match '^Default Gateway' -replace $regex,'$1'

    New-Object psobject -Property @{
    Gateway= $gateway
    File = $filename

    } |
    select Gateway,File |
    Export-Csv C:\test\gateways.csv -NoTypeInformation

    Edit: replaced $file variable refs inside the loop with $filename

The topic ‘Wildcard text search’ is closed to new replies.