Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing tabs to a file using PowerShell

Tags:

I need to echo a series of elements of an array in PowerShell, but provide various delimiters between the elements, so I'm using;

    Add-Content -Path $tempInputDir\testoutput.log -value ($($fields[0]) + "   "+         $($fields[1])  + "    " + $($fields[2]) + " " + $($fields[3])    + "    "+         $($fields[15]) + "    " + $($fields[17])) } 

I need to be able to add tabs and space characters, as you can see from the code above I've just done this by physically adding tabs and spaces in between double quotes, but I'm sure this will cause problems down the line.

What's the correct way to echo these characters to a file? I read somewhere that "'t" could be used, but that doesn't seem to work?

like image 253
Andy Walker Avatar asked Mar 23 '09 01:03

Andy Walker


2 Answers

You can use `t for a tab character in a double quoted string. You can also simplify the above to:

"$($fields[0])   $($fields[1])   $($fields[2]) $($fields[3])  $($fields[15])  $($fields[17])" | Add-Content $tempInputDir\testoutput.log 
like image 167
Keith Hill Avatar answered Oct 25 '22 22:10

Keith Hill


To join the nominated fields together with tabs:

[string]::join("`t", (0..3,15,17 | % {$fields[$_]})) 
like image 25
dan-gph Avatar answered Oct 25 '22 23:10

dan-gph