restrict select-string to certain columns of a text file

Welcome Forums General PowerShell Q&A restrict select-string to certain columns of a text file

This topic contains 2 replies, has 3 voices, and was last updated by

8 months, 1 week ago.

  • Author
  • #95361

    Points: 0
    Rank: Member

    I am working with very large text files, consisting of very log lines (> 1000 characters per line). Using select-string to find a 10-character string in these files takes a couple of minutes. I am trying to find ways to optimize the search.Is there a way that I could restrict the search to certain characters of each line in the text file. For example, I might know that the string I am searching for is in position 350 to 352. Thanks for any assistance.

  • #95398

    Points: 206
    Helping Hand
    Rank: Participant

    Working with large text files means, do not use Get-Content.

    Why Get-Content Ain't Yer Friend

    Use the .Net file libraries.


    How are you using Select-String?

    Have you looked at the speed difference between Select-String and plain RegEx match?

  • #95430

    Points: 202
    Helping Hand
    Rank: Participant

    I'm not sure if this is faster but:

    get-content text | foreach { $_.substring(349,361) } | select-string whatever10

    Have you tried get-content's -readcount parameter?

The topic ‘restrict select-string to certain columns of a text file’ is closed to new replies.