8

How do I test if the first line of a text file is terminated with \r or \n?

I tried various renditions similar to the following. I'm not sure that the string imported into powershell (first line of the file) even contains the invisible characters. I also tried using the StreamReader method to read the line to no avail.

$master = Get-Content $masterFilename | Select-Object -First 1
if ($master.Contains("\r|\n"))
    {
        write-host "you betcha, there's a carriage return or line feed"
    }

Thank you.

STWilson
  • 1,538
  • 2
  • 16
  • 26

1 Answers1

9

Get-Content converts your file into a sting array based on those characters. By the time you start to test the contents those characters have been stripped out. If you are just looking for the presence of newlines (your condition does not seem to care what it encounters) testing the amount of lines returned from Get-Content would be enough.

If((Get-Content $masterFilename).Count -gt 1){"you betcha, there's a carriage return or line feed"}

I'm sure I will add to this once you see it. I think you might need to revise your question to be a little more specific.

Get-Content has a -Raw switch which would preserve the new line delimiters which is closer to where you want to be going. I'm not sure the string method contains supports wildcards. I do know that it does not support regex.


If you were just curious about one line then as mentioned above -Raw would be the place to start. Consider the following text file represented in hex. It is just the word hello followed by a newline.

68 65 6c 6c 6f 0d 0a

Now I will read that text in as one string and test if the end of the string has a carriage return and line feed. Note that I am not escaping with backticks. I am using regex special characters. (It would work either way. Just something to be aware of.)

(Get-Content c:\temp\test.txt -Raw) -match "\r\n$"
True

Note that most of the PowerShell cmdlets that write to file leave a trailing newline. So no matter how many lines you have the above code snippet would be true for something like Set-Content.

Matt
  • 45,022
  • 8
  • 78
  • 119
  • What happens if you had only one line in the file terminated with a carriage return or line feed? – DanL Feb 12 '16 at 07:59
  • Thanks for the additional edit. Your last snippet is my solution! I appreciate that, Matt. – STWilson Feb 12 '16 at 13:24
  • I cannot find a way around -Raw loading a large file in entirety. It seems using `Get-Content c:\temp\reallybigfile.csv -Encoding byte -ReadCount 1000 | Select-Object -First 1` in my case grabs at least the first line, then testing that array for `10 or 13`, the Dec codes of LF and CR may be my final answer. – STWilson Feb 12 '16 at 15:27
  • 1
    @STWilson What are you looking for in this large exactly? `[System.IO.File]::ReadAllText()` or maybe `[System.IO.File]::ReadAllBytes()` are faster than using `Get-Content`. – Matt Feb 12 '16 at 15:31
  • `([System.IO.File]::ReadAllBytes($aBigOne) | Select-Object -First 1000) -Contains 10` is fast and elegant. Limiting saves a fruitless search of the huge file in case no match in the first line. – STWilson Feb 12 '16 at 16:21
  • My scenario is: whether lines terminate with CR get reflected in an SQL query called from PowerShell. – STWilson Feb 12 '16 at 16:29