1

I am trying to filter a large log file. Is there a way to read a line from the log, if the line contains less than 2 characters, delete the entire line from the log using power-script

I have come up with a way of counting characters in the file

  Get-Content ./output.txt | ForEach-Object { $_ | Measure-Object -Character } | Out-File count.txt 

This counts each line and then outputs the characters counted into another file

And I know how to delete an empty line

  Get-Content .\output.txt | where {$_ -ne ""} | Set-Content out.txt

or a line that contains a specific character or string

  Get-Content .\in.txt | Where-Object {$_ -notmatch 'STRING'} | Set-Content out.txt

Is there a way to pipe the output and ask "if the count is <=1 delete that line from the log"

Basically

    for each line
      if line is <= 1 delete line
      else leave alone

I hope this makes sense to you guys, I find it hard to get out whats in my head sometimes in a way that makes sense to others. Any help would be much appreciated

2 Answers 2

3

$_ is a [string]/System.String and a string has properties and methods of which one tells us the length of the string.

Get-Content .\output.txt | where {$_.Length -gt 1} | Set-Content out.txt
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Loads :) And it makes sense
2

If you want to use a regular expression to remove lines with less than 2 characters, you can do the following:

Get-Content .\in.txt | ? { $_ -match '..' } | Set-Content .\out.txt

The expression .. matches strings with at least 2 characters (not including newlines).

2 Comments

Brilliant, it worked. Thanks loads. I'm not sure how or why it works but I'll work it out
Get-Content returns the contents of the file as an array of lines. The regular expression allows all lines with 2 or more characters to pass and discards the rest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.