Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to out-file to unix style line ending

6,256 views
Skip to first unread message

Xah Lee

unread,
Jul 24, 2009, 6:03:16 PM7/24/09
to
how to get a output to have unix styled newline?

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/


Alex K. Angelopoulos dot dot again at

unread,
Jul 25, 2009, 5:54:24 AM7/25/09
to
The cmdlets don't work very well for this since they generally treat files
as lists of lines - and therefore hide line termination details. Your best
bet for creating Unix-style line terminations is to ignore the cmdlets.
Instead, join the array of lines into a single text blob yourself, then
write them to the file using either a .NET streamwriter or a COM object like
Scripting.FileSystemObject. Here's a demo of how I would do this. NOTE: you
want to provide a complete path to the target file you're writing; relative
paths using a . or .. won't be expanded relative to PowerShell location when
using a component that isn't PowerShell-native.

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...

Xah Lee

unread,
Jul 25, 2009, 10:22:15 AM7/25/09
to
hi Alex,

Thanks a lot for the answer.

Xah

On Jul 25, 2:54 am, "Alex K. Angelopoulos" <alex(dot) k(dot again)

Joel Bennett

unread,
Jul 25, 2009, 2:30:53 PM7/25/09
to

You know, the most trivial way to do this is to just use Out-String and
Out-File together, instead of writing your own text-stream handler

... |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__Weiss

unread,
Jul 25, 2009, 3:14:24 PM7/25/09
to
Also getting in the way of using > for your needs is the fact that the
PowerShell output redirect operator > always produces UNICODE encoding.

- Larry

Xah Lee wrote:
> how to get a output to have unix styled newline?

> ...

Joel Bennett

unread,
Jul 26, 2009, 12:05:23 AM7/26/09
to
unicode isn't a problem though, right?

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

Xah Lee

unread,
Jul 26, 2009, 6:34:07 AM7/26/09
to
On Jul 25, 11:30 am, Joel Bennett <Jay...@HuddledMasses.org> wrote:
> You know, the most trivial way to do this is to just use Out-String and
> Out-File together, instead of writing your own text-stream handler
>
> ... |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 ...

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/


Flowering Weeds

unread,
Jul 26, 2009, 10:25:41 AM7/26/09
to

Mmm since this is the year of data parsing
and charting (and yes Regex is also included)
then perhaps for large files:

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

unread,
Jul 26, 2009, 7:54:58 PM7/26/09
to
Actually, that's an interesting question. I suppose that Out-File always
appends a line break (that makes sense), but what about Add-Content or
Set-Content? I don't have time to play with it right now.
--
Joel

Alex K. Angelopoulos dot dot again at

unread,
Jul 28, 2009, 9:30:55 AM7/28/09
to
The (Add)|(Set)-Content both automatically use a CRLF combo to terminate
each line, including single line entries.

"Joel Bennett" <Jay...@HuddledMasses.org> wrote in message
news:O3ARkykD...@TK2MSFTNGP04.phx.gbl...

datavalue

unread,
Sep 8, 2009, 4:51:01 AM9/8/09
to

I did the following (Shoved the output into .Net):


# 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()

0 new messages