tags:

views:

326

answers:

1

Have a very simple powershell script to count the number of trades in a file:

(gc \mimkeimc11n\Batch\FundQuest\TradeFiles\trades.dat |? {$_ -match 'SL|BY'} | Measure-Object | select count).count > \mimkeimc11n\Batch\FundQuest\ConfirmtoFQ\NumberofTrades.txt

The problem I am running into is the output NumberofTrades.txt is including the number that I want, but also a CR LF, not sure why??? Any help would be greatly appreciated.

http://screencast.com/t/MGM3ZTc0Mzct

+1  A: 

PowerShell is pretty persistent about outputting newlines for you when you send strings to Out-File (alias >) or even Add/Set-Content. It can be infuriating sometimes and makes me wish for a -NoNewLine parameter on these cmdlets. For now you can use a .NET API e.g.:

$path = '\mimkeimc11n\Batch\FundQuest\ConfirmtoFQ\NumberofTrades.txt'
(gc \mimkeimc11n\Batch\FundQuest\TradeFiles\trades.dat | 
    ?{$_ -match 'SL|BY'} | Measure-Object).count |
 %{[IO.File]::WriteAllText($path, $_)}
Keith Hill
thanks Keith this did the trick, and i couldn't agree more about the -nonewline, @ least give us some option
duhaas
If anybody agrees with this need then vote on the suggestion at https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=524739
Keith Hill