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

js
 
Participant
8 months, 1 week ago.

  • Author
    Posts
  • #95361

    Participant
    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

    Participant
    Points: 206
    Helping Hand
    Rank: Participant

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

    Why Get-Content Ain't Yer Friend
    'powershell.org/2013/10/21/why-get-content-aint-yer-friend'

    Use the .Net file libraries.

    [System.IO.File]::ReadLines

    How are you using Select-String?
    -Pattern
    -SimpleMatch
    -AllMatches

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

  • #95430
    js

    Participant
    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.