What is the `tr` command in Windows?
What is the Windows equivalent of the tr
command in Linux?
For example, tr
can replace all colons with a newline character. Can this be done in Windows?
$ echo $PATH | tr ':' '\n'
/usr/local/bin
/usr/bin
Solution 1:
in powershell we have split
see this example
$a=( echo $env:Path | Out-String )
$a -split ";"
before :
%SystemRoot%\system32\WindowsPowerShell\v1.0\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System
32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Prog
ram Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\Micros
oft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\010 Editor;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web P
ages\v1.0\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft\Web Platform
Installer\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft Visual Studio\Common\Tools\Win
NT;C:\Program Files (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files (x86)\Microsoft Visual Studio\Com
mon\Tools;C:\Program Files (x86)\Microsoft Visual Studio\VC98\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Com
mon Files\Intel\WirelessCommon\
After:
> %SystemRoot%\system32\WindowsPowerShell\v1.0\ C:\Windows\system32
> C:\Windows C:\Windows\System32\Wbem
> C:\Windows\System32\WindowsPowerShell\v1.0\ C:\Program
> Files\Intel\WiFi\bin\ C:\Program Files\Common
> Files\Intel\WirelessCommon\ C:\Program Files (x86)\ATI
> Technologies\ATI.ACE\Core-Static C:\Program Files
> (x86)\QuickTime\QTSystem\ C:\Program Files\Microsoft SQL
> Server\110\Tools\Binn\ C:\Program Files (x86)\010 Editor C:\Program
> Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\ C:\Program Files
> (x86)\Windows Kits\8.0\Windows Performance Toolkit\ C:\Program
> Files\Microsoft\Web Platform Installer\
> C:\Windows\System32\WindowsPowerShell\v1.0\ C:\Program Files
> (x86)\Microsoft Visual Studio\Common\Tools\WinNT C:\Program Files
> (x86)\Microsoft Visual Studio\Common\MSDev98\Bin C:\Program Files
> (x86)\Microsoft Visual Studio\Common\Tools C:\Program Files
> (x86)\Microsoft Visual Studio\VC98\bin C:\Program
> Files\Intel\WiFi\bin\ C:\Program Files\Common
> Files\Intel\WirelessCommon\
Solution 2:
there is an ugly batch-hack:
echo "%path:;="&echo "%"
Solution 3:
The equivalent of tr in a modern Windows O/S (Windows 7 upwards) is:
powershell -noprofile -command "$Input | foreach { write-output $_.Replace(<from-string> , <to-string> )}"
So, in your case:
path | powershell -noprofile -command "$Input | foreach { write-output $_.Replace(';',\"`r`n\")}"
Alternatively, just install Cygwin to get most unix commands.
Solution 4:
If you have Windows Subsystem for Linux (WSL) enabled, you can simply call wsl tr
under PowerShell or CMD.
WSL allows one to call Linux commands from a Windows command prompt with only a minor performance penalty. It's been a feature of Windows 10 for several years now, and simply needs to be enabled.
Simple example:
PS> Write-Output "1:2:3x" | wsl tr ':' ',' | wsl tr -d 'x'
1,2,3
To recreate the OP's Linux example in CMD:
C:\> echo %path% | wsl tr ";" "\n"
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
...
From PowerShell, there's a little more quoting/escaping involved:
PS> echo $env:Path | wsl -d TmpTumble tr "\;" "\\n"
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
...