View Single Post
Old 12-02-2007   #4 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: stdout redirection

"Kuma" <kumasan76@xxxxxx> wrote in message
news:df04d430-83b5-4e64-a47e-7bcfa100d6f8@xxxxxx
Quote:

> On Dec 3, 7:35 am, tomaszinc <tomasz...@xxxxxx>
> wrote:
Quote:

>> Hello everyone,
>> I'm pretty new to powershell scripting so this might seem like a stupid
>> question, excuse me for that. I already searched the forum and
>> documentation
>> but couldn't find a solution so I take my chances here.
>>
>> Basically all I want to do is to write the output of some command line
>> utilities and python scripts into a text file. With cmd I used the '>'
>> operator for that. However, the resulting file is messed up with spaces
>> between each character. I searched the documentation and found info about
>> the
>> commands 'out-file' and 'set-content'. There are a number of different
>> encodings which can be set. I tried every combination of {out-file,
>> set-content} -encoding *, however, the outfile never matches the output
>> generated on the console. Instead either spaces or new-lines are
>> inserted.
>> Since I use this output for further processing it is vital that the file
>> exactly matches the output on stdout.
>> Can anybody tell me how I can redirect the output on stdout to a file
>> without powershell messing with the content?
>>
>> best regards and thanks a lot
>
> Take a look here:
>
> http://keithhill.spaces.live.com/blo...3A97!811.entry
>
> Excellent page for this topic.
Thanks. This particular issue can be annoying. Obvious when you send an
array of strings down the pipe (where they're not CRLF terminated) and you
redirect that to a file, you would want each string to correspond to a line
in the file. That requires PowerShell to append it's own CRLF to the end of
each line even if the string already has a CRLF at the end e.g.:

126# "one`r`n","two`r`n","three" | Out-File .\output.txt -Encoding ascii
127# format-hex C:\Temp\output.txt

Address: 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15
16 17 ASCII
-------- -----------------------------------------------------------------------
------------------------
00000000 6F 6E 65 0D 0A 0D 0A 74 77 6F 0D 0A 0D 0A 74 68 72 65 65 0D 0A
one....two....three..

NOTE: Format-Hex is a PSCX cmdlet. I wonder if it would have made more
sense to have PowerShell punt on adding CRLF when the string already ends in
a CRLF. Hmmm. Anyway you can do what you want. Unfortunately it isn't as
straight forward as you would hope:

128# $output = csc.exe
129# $text = [String]::Join([Environment]::NewLine, $output)
130# [System.IO.File]::WriteAllText("$pwd\foo.txt", $text)

For what it's worth, if you use the WriteAllLines method in the File class,
which takes an array of strings, you wind up with the exact same problem -
an extra CRLF at the end.

--
Keith

My System SpecsSystem Spec