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

How to remove blanks line from a text outpu file

1,443 views
Skip to first unread message

cosimo

unread,
Aug 21, 2008, 2:05:02 PM8/21/08
to
If I wrote a script file like this :
Get-Date | format-list DayOfWeek | out-file -Encoding ascii date.txt

It write Day of week in date.txt text file but the output text file starts
with two blank line (like Carriage Return) and then : "DayOfWeek : Thursday"
Is there any way to format the output text file so the "DayOfWeek :
Thursday" starts at the first line of the text file (without blank lines)?

Thanks in advance.
Ing. Cosimo Mercuro

Marco Shaw [MVP]

unread,
Aug 21, 2008, 2:21:25 PM8/21/08
to

Yes, there are annoying blank lines that will show up with the various
format-* and select-object cmdlets, for example.

Quick workaround:
PS>"DayOfWeek: "+(get-date).dayofweek|out-file -Encoding ascii date.txt

Marco

--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com

Kiron

unread,
Aug 21, 2008, 4:10:03 PM8/21/08
to
# in PowerShell blank lines are equivalent to $false when cast
# as a [boolean]
[bool]'' -eq $false

# if you pipe the formatted objects to Out-String with its
# -Stream switch 'on' you could then filter out the blanks
# through Where-Object:

get-date | format-list DayOfWeek | out-string -stream |
? {$_} | out-file -encoding ascii date.txt
gc date.txt

# another way is to remove the blank lines from the file
# and set the content back to it:

get-date | format-list DayOfWeek | out-file -encoding ascii date.txt
# before
gc date.txt
set-content date.txt (${c:date.txt} | ? {$_}) -encoding ascii
# after
gc date.txt


# to remove all blank lines but leave the top, or the bottom, ones
get-date | format-list DayOfWeek | out-file -encoding ascii date.txt

# remove all blank lines, except for the bottom
(gc date.txt | out-string) -replace '(\r\n)+',"`r`n" -replace '^(\r\n)+'

# remove all blank lines, except for the top
(gc date.txt | out-string) -replace '(\r\n)+',"`r`n" -replace '(\r\n)+$'

--
Kiron

tojo2000

unread,
Aug 21, 2008, 7:01:16 PM8/21/08
to

Just for the sake of one more way to do it:

Get-Date | %{"DayOfWeek: $($_.DayOfWeek)"} | out-file -Encoding ascii
date.txt

cosimo

unread,
Aug 22, 2008, 5:20:03 AM8/22/08
to
Thanks Marco.
It works fine!
Cosimo Mercuro

awfisms

unread,
Nov 16, 2008, 7:00:01 AM11/16/08
to
One more, surely:

"DayOfWeek: $((get-date).dayofweek)" | out-file -Encoding ascii date.txt

0 new messages