I wanted to do something like this:
get-content $inputFile | select-string 'something' | ... | out-file -
Encoding utf8 -width 999000 log_report.txt
I'm hoping that out-file has a option something like “-newline unix”,
but doesn't.
I tried this:
get-content testFile.txt | &{$input -replace "'r'n","'n" } >
testFile2.txt
but that doesn't work. Apparently, the redirect operator again made it
DOS line endings.
Am using powershell 2.0.
am unix sys admin & programer for years. New to Windows. Thanks.
Xah
∑ http://xahlee.org/
☄
Example:
# Get the data and process it; this would be everything
# before Out-File ... in your example:
$data = get-content $inputFile | select-string 'something' | ...
# $data is an array; we can use System.String's static Join() method
# to merge it into a text blob with UNIX line endings;
# data will now be a single chunk of text:
$data = [System.String]::Join("`n",$data)
# We COULD write this to a file using set-content, but the final
# line would have a \r\n line termination, which you don't want.
# Instead, we create a COM textstream object to use as a writer.
$fso = New-Object -ComObject Scripting.FileSystemObject
$file = $fso.Open
# Generate log_report's path relative to current PowerShell location:
$path = "$PWD\log_report.txt"
# Constants needed for opening file;
# $TristateTrue forces Unicode, $CreateFile for forcing
# file creation if it doesn't exist.
$ForWriting = 2; $TristateTrue = -1; $CreateFile = -1
#Now open a textstream for writing to the file:
$textstream = $fso.OpenTextFile($path , $ForWriting, $CreateFile,
$TristateTrue)
$textstream.Write($data)
# if you need a final line termination, use the
# following instead:
# $textstream.Write("$data`n")
# now close the file:
$textstream.Close()
"Xah Lee" <xah...@gmail.com> wrote in message
news:f6b7bdff-b638-4fb2...@13g2000prl.googlegroups.com...
Thanks a lot for the answer.
Xah
On Jul 25, 2:54 am, "Alex K. Angelopoulos" <alex(dot) k(dot again)
... |Out-String -Stream|&{[String]::Join("`n",@($input))}| Out-File ...
OR you can let Out-String join them, and then replace them
... | Out-String | %{ $_.Replace("`r`n","`n") } | Out-File ...
If you manually join them like that, Out-File will just output the whole
thing. You can do it as a function too:
function Join-String ($separator=$ofs) {
[String]::Join($separator,@($input) )
}
Do-Stuff ... | Out-String -Stream | Join-String "`n" | Out-File ...
--
Joel
- Larry
Xah Lee wrote:
> how to get a output to have unix styled newline?
> ...
You just need to use Out-String before you try to replace the line endings.
This thing is a noop; it doesn't DO anything:
$input -replace "'r'n","'n"
--
Joel
Thanks. I tried this last one, it seems to work fine. Only little
problem is that the last line is a Return char... Seems inevitable
with out-file at the end.
Is there a way to fix that?
i wanted unix newlines because i just migrated from unix and unix
tools pretty much all assume utf8 and unix line ending. Most tools has
no options to change interpretation.
Xah
∑ http://xahlee.org/
☄
FYI
>
> I wanted to do something like this:
>
> get-content $inputFile | select-string 'something' | ... | out-file -
> Encoding utf8 -width 999000 log_report.txt
>
Be aware Windows-based Windows
PowerShell is not a data parser,
but is a Windows-based automation tool,
meant to "move data"
from tool to tool, to tool,
until output (or whatever)!
> i wanted unix newlines because i just
> migrated from unix and unix tools
Mmm automate tools and there really are
lots of Windows-based tools available,
for Windows PowerShell to automateI
So for here perhaps (like so many IT Pros)
automate a data parser tool within powershell.exe!
And the one Windows-based
data parsing tool used within
PowerShell (a lot) for a lot of
parsing (registry, file system,
files, arrays, memory, COM
and .NET scriptable and even
add one's own parsing needs)
"Log Parser" - Bing
http://www.bing.com/search?q=%22Log+Parser%22
And an added benefit,
after all these years,
is that any PowerShell user
(if they care to)
can help one
automate Log Parser too!
Remember sooner or later
all Powershell users will need
to data parse larger data / files.
So it's best to learn Log Parser
now (within PowerShell) with
one's much small data parsing
needs. Plus Log Parser runs in
almost any Windows process
(for even easier learning)!
And also perhaps use the built-in
PowerShell BareFootin' WPF
to GUI display one's data or
use one of the many PowerShell
ways to chart one's data too!
As always enjoy the automation
of tools within the Windows-based,
.NET aware, WPF accessible,
multi-processes on the same IP / Port usage,
admin's automation tool,
powershell.exe!
"Joel Bennett" <Jay...@HuddledMasses.org> wrote in message
news:O3ARkykD...@TK2MSFTNGP04.phx.gbl...
# Force the lineResults to Unix format and save to a file
$streamWriter = New-Object
System.IO.StreamWriter("$(get-location)\data.txt", $false)
[System.String]$lineString = $lineResult
# Line will be delimited by spaces so replace with NewLineFeed
[System.String]$convertLine = $lineString.Replace(" ","`n")
$streamWriter.Write($convertLine)
$streamWriter.Flush()
$streamWriter.Close()