LoginSignup
22
22

More than 5 years have passed since last update.

PowerShellでファイルにリダイレクトしたときに勝手に改行される場合の対処

Last updated at Posted at 2017-02-14

PowerShellでリダイレクトを使ってファイル出力すると、一定の長さ(PowerShellの画面バッファーの設定に依存するようです)で改行されてしまう。

例えば、テキストをフィルタリングして、別のファイルに書き出す次のようなコマンドを実行すると、sample_out.txtでは、長い行が勝手に改行されてしまっている。

cat .\sample.txt | sls hoge > sample_out.txt

この問題は、以下のようにOut-Fileコマンドレットを使うと解消できる(※1000は十分に大きな数)。

cat .\sample.txt | sls hoge | Out-file -filepath sample_out.txt -width 1000

(なんでリダイレクトしたいだけなのに、こんな長いコマンドを書かないといけないの・・・)

追記

コメントで教えて頂きました。
slsの返すMicrosoft.PowerShell.Commands.MatchInfoのリダイレクトが問題の原因であるため、

(type sample.txt | sls hoge).ToString() > sample_out.txt

または

type sample.txt | sls hoge | % { $_.ToString() } > sample_out.txt

と書いても良いようです。
こちらの方が -width 1000 のような固定値が入らない分、スマートですね。

22
22
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
22