0

Trying to search through .txt files and find line that contain any value higher than 0.

(Select-String -Path SADM-COUNT\*.txt -AllMatches '0*[1-9]\d*$').Line | Set-Content Output.txt

sample of the .txt file we are searching:

Fa0/1        12479523313       18114137          73428              7 
Fa0/2                  0              0              0              0 
Fa0/3                  0              0              0              0 

I just want to see lines in the file that have lines with numbers higher than 0.

Any help would be greatly appreciated,

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
normbeef
  • 27
  • 3
  • 1
    `(Get-Content FILE) -match '\s[1-9]'` – Ansgar Wiechers Mar 13 '18 at 13:15
  • works a treat.. thankyou so much.. would you know how to include the Filename in the output, as we are searching multiple files at the same time? – normbeef Mar 13 '18 at 13:27
  • @normbeef: Please update your question directly with the new requirement (in more detail - where should the filename go?). Please note that, generally, you shouldn't change the premise of the question after the fact, especially if answers have already been given. – mklement0 Mar 13 '18 at 13:44
  • Hi, yes apologies.. the solution worked perfectly, I have found a solution to the other question using Perl. Many thanks for the help. – normbeef Mar 13 '18 at 14:00
  • Glad to hear it. I actually meant if answer _posts_ have been created yet. @AnsgarWiechers' answer has the form of a _comment_, so it's not too late to modify the question in this case - assuming that you still have interest in an all-PowerShell solution. If not, I hope that Ansgar publishes his comment as an answer, so we can get closure. – mklement0 Mar 13 '18 at 14:19

1 Answers1

0

Check for whitespace followed by a non-zero digit:

(Select-String -Path 'SADM-COUNT\*.txt' -AllMatches '\s[1-9]').Line |
    Set-Content 'Output.txt'

You don't need Select-String for this, though. Get-Content and the -match operator will suffice:

(Get-Content 'SADM-COUNT\*.txt') -match '\s[1-9]' | Set-Content 'Output.txt'
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328