Friday, February 8, 2013

Concatenate lines in a file into a single line with PowerShell

Assuming you have a input file in.txt with following lines:


and you'd like to concatenate these 3 lines into a single line to a file out.txt i.e.


You can use the following PowerShell command:

-join (cat in.txt) | out-file out.txt

Basically, the cat in.txt is executed first and returned the content of in.txt as an array of strings. The -join cmdlet then joins the lines together into a single line, which is then piped (|) into the out-file cmdlet to produce the out.txt file.