2

I applied several filters to a text files using Powershell Get-Content and the -nomatch operator and then i spooled the result to a file.

gc file.txt | {?_ -notmatch 'excl1|excl2|excl3'} | out-file newfile.txt

What happens is that the output file (newfile.txt) has less lines, but it is reported by windows with a bigger size than file.txt.

Has someone ever encountered this behavior? How can I have the correct size reported by windows? I checked the number of rows, the file with less rows is reported as bigger in size.

5
  • 1
    filter a text file (.txt) using several keywords in the format: gc file.txt | ?{$_ -notmatch 'excl1|excl2|excl2'} | out-file newfile.txt. newfile.txt is bigger than file.txt Commented Dec 21, 2015 at 17:26
  • 1
    {?_ -notmatch 'excl1|excl2|excl3'} is the filter. Commented Dec 21, 2015 at 17:42
  • 1
    Give us an example file with your comparing file sizes. .... Also need to know the encoding. I am guessing that is where the issue comes from? Commented Dec 21, 2015 at 17:58
  • 2
    I agree with Matt. The output file is probably larger because Out-File creates the file with Unicode encoding (2 bytes per character) whereas the input file most likely is ASCII encoded (1 byte per character). Commented Dec 21, 2015 at 18:10
  • Where is Select-String? Commented Dec 21, 2015 at 20:23

1 Answer 1

3

I'm certain you have an encoding issue. By default Get-Content uses ascii whereas Out-File uses Unicode.

From TechNet

-Encoding

Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode", "Default", and "OEM". "Unicode" is the default.

Use -Enconding ascii with Out-File or just use Set-Content as it is the partner of Get-Content.

Get-Content file.txt | {?_ -notmatch 'excl1|excl2|excl3'} | 
    out-file -Encoding ascii newfile.txt
    # or
    Set-Content newfile.txt

Coming from the other direction if you have issues with your input file Get-Content in PowerShell v3 and above also supports -Enconding

Sign up to request clarification or add additional context in comments.

Comments

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.