January 1, 2012 at 12:00 am #5041
by stocksp at 2012-08-23 10:23:20
Not sure exactly what I'm seeing.
I have two strings that are elements of a collection of strings (HTML's)
PS says they have no CRLF on the end
If I send this guy to a file. The single line has a CRLF at the end.
This CRLF at the end of the string becomes a problem as I add a CRLF between elements that I want grouped together.
Later I -split the strings to get the elements back, but I end up with an extra blank element from the CRLF that PS seems to add.
I don't ever see the CRLF at the end of the string in functions where I muck with the text. It only shows its face on return from the function or if I send the strings to a file.
How can I avoid this terminal CRLF
thanks in advance for any help!
by willsteele at 2012-08-23 10:35:43
Although it is only indirectly related, I wrote a post yesterday because of this CR/LF issue partly because of the other thread. Maybe this will help you get some background information to help understand what you are dealing with: http://learningpcs.blogspot.com/2012/08/powershell-v1v2v3-weirdness-with-file.html. An alternative may be to load a hex editor and look at the end of line characters to see exactly what you are dealing with in those files. I use HxD to look at the binary data in a file: http://mh-nexus.de/en/hxd/. Sometimes it is important to check and see if you are dealing with CR/LF for sure. Also, if you can post some of the data you are working with that may help us to get you an answer.
by stocksp at 2012-08-23 10:45:00
My editor shows the end of line characters.
The problem\issue is simplified here.
$x = "testing"
$x += $x
Sx > x.txt
"testingtestingCRLF" in the output file.
thanks for the link
by willsteele at 2012-08-23 10:54:26
So, in the file you are working with, you are not seeing CR/LF characters (`r`n or 0x0D,0x0A) at the end of the lines? Is that correct?
Also, have you considered working with the data as XML instead of plain ASCII text? A lot of these issues can be bypassed, depending on how your page is formatted, if you handle it as XML.
by stocksp at 2012-08-23 11:16:32
I DO see the CRLF at the end of the file OR the returned output from a function.
I've solved the problem by using a "|" symbol to concat and split my strings.
Its just ugly, and I was trying to understand WHY I'm having problems with `r`n.
"testing" > x.txt
The file has a CRLF at its end.
"testingCRLF" is what I see in the output.
How can I get rid of these two characters I didn't add?
by willsteele at 2012-08-23 11:28:06
Maybe I am missing something, but, any time you create a new line in windows, the two control characters, CR and LF, are automatically inserted to create this "new line". That is just how Windows handles the line breaks. MAc and Unix handle it differently, as I noted in my link, but, control characters are required to force the new line.
by stocksp at 2012-08-23 11:52:30
Thanks for your help.
I didn't know that CRLF are always added to the end.
Maybe my "|" isn't a hack.
If I can't control the terminal character then I guess I can't use that character(s) (CRLF) as the -split character.
This particular data set (very ugly HTML) will never have this symbol (pipe).
Too bad, the CRLF is so much easier to debug because the editor does a line break. With the "|" I don't get the 'free' formatting for debugging.
I'll revist this when i have time, maybe there is something wrong with my code...
thanks again for your patience
by poshoholic at 2012-08-23 13:54:30
No, no, that's not right...Will, you were missing the root question that was being asked. stocksp was asking this:
"How do I redirect output into a text file in PowerShell such that it does not finish with a newline character?"
The answer to that question is to use the .NET WriteAllText static method of the System.IO.File class, like this:
Once you have created such a file, you can verify view the contents (including CR and LF) like this:
[script=powershell][System.IO.File]::ReadAllBytes('C:\x.txt') -join ' '[/script]
This is not a hack. It is a .NET-based solution to the issue that exists when redirecting output to a file via the redirect operator or via Out-File, where a [System.Environment]::NewLine is appended to the end of the file in both cases. Most of the time you probably don't care about the newline. But when you do, it's good to know how to avoid it.
If you're dealing with an array, and you do want newline characters in between the elements of the array, you can do that too, like this:
[script=powershell]$array = @('test1','test2')
[System.IO.File]::WriteAllText('C:\x.txt', $array -join [System.Environment]]
Then, if you verify the contents of that file using the tecnique provided earlier, you'll get this in PowerShell (CR and NL characters marked in bold; note, there are none at the end of the file):
PS C:\> [System.IO.File]::ReadAllBytes('C:\x.txt') -join ' '
116 101 115 116 49 13 10 116 101 115 116 50
This should resolve your issue.
by willsteele at 2012-08-23 14:07:59
Thanks for catching it Kirk. I felt like I was missing something, but, wasn't quite sure what the underlying question was.
You must be logged in to reply to this topic.