16

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
SATO Yusuke
  • 1,600
  • 15
  • 39
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
  • 2
    I don't get it - do you know the Windows command and are asking for the Linux equivalent, or know Linux command and are asking for the Windows one? – Amadan Mar 27 '15 at 01:52
  • 1
    As an "eliminate a whole class of errors from my code" practice, you always want to dbl-quote your variable names, i.e. `echo "$PATH" | ...` Good luck. – shellter Mar 27 '15 at 02:00

8 Answers8

8


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\
Soheil
  • 837
  • 8
  • 17
8

there is an ugly batch-hack:

echo "%path:;="&echo "%"
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    @RaúlSalinas-Monteagudo it echoes the `%path%` variable, but replaces each `;` with `"&echo "`, resulting in a couple of `echo` commands. Put the line into a batch file (with `echo on`) to see the effect. – Stephan Mar 11 '19 at 15:23
8

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\
...
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
Wolfgang Grinfeld
  • 870
  • 10
  • 11
  • **WSL One-Liner:** `if (!([bool](Get-Command -Name tr -EA SilentlyContinue))) {if ([bool](Get-Command -Name wsl -EA SilentlyContinue)) {Function AliasTr {wsl tr $args};Set-Alias tr AliasTr}}` – PotatoFarmer Jul 29 '21 at 00:04
5

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.

Wolfgang Grinfeld
  • 870
  • 10
  • 11
  • According to https://stackoverflow.com/questions/325953/how-can-i-replace-newlines-using-powershell/326082#326082, you really only need `\`n` in the Replace – DrStrangepork Nov 27 '18 at 15:52
4

Yes, there is no drop in replacement. To split the PATH variable into lines on Windows PowerShell use:

PS >$env:Path -split ';'
C:\WINDOWS\system32
C:\WINDOWS
...

Or replace the delimiter with explicit new line (CRLF):

PS >$env:Path -creplace ';', "`r`n"
C:\WINDOWS\system32
C:\WINDOWS
...

Notes:

  • Backticks are not evaluated in single-quoted strings.
  • Powershell works on MacOS and Linux too. But there are differences. To write plattform independent, your code might look like this: $env:PATH -creplace [IO.Path]::PathSeparator, [Environment]::NewLine
  • Using RegEx with -replace: https://ss64.com/ps/syntax-regex.html
3

As far as I've learned, there isn't a drop in replacement for tr, but this works simply and memorably for your use case (in PowerShell):

echo $env:PATH.Replace(';',"`r`n")
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
2

There is no straightforward tr equivalent in Windows, so I made an cmdlet that you can use like tr command (from: http://satob.hatenablog.com/entry/2019/06/18/013811):

function tr {
  Param(
    [Parameter(ValueFromPipeline=$true,Mandatory=$true)]
    [string] $TargetString,
    [Parameter(Mandatory=$true)]
    [string] $FromString,
    [Parameter(Mandatory=$true)]
    [string] $ToString
  )
  begin {
    # Split string into character array
    # [-split ""] splits a surrogate pair into two invalid characters,
    # so the code below is not suitable for this purpose
    # $FromStringArray = $FromString -split "";
    # $FromStringArray = $FromStringArray[1..($FromStringArray.length-2)];
    $FromStringArray = @();
    $FromStringBytes = [Text.Encoding]::UTF32.GetBytes($FromString);
    for ($i=0; $i -lt $FromStringBytes.length; $i+=4) {
         $FromStringArray += [Text.Encoding]::UTF32.GetString($FromStringBytes, $i, 4);
    }

    $ToStringArray = @();
    $ToStringBytes = [Text.Encoding]::UTF32.GetBytes($ToString);
    for ($i=0; $i -lt $ToStringBytes.length; $i+=4) {
         $ToStringArray += [Text.Encoding]::UTF32.GetString($ToStringBytes, $i, 4);
    }
  }
  process {
    for ($i=0; $i -lt $FromStringArray.Length -and $i -lt $ToStringArray.Length; $i++) {
        $TargetString = $TargetString.Replace($FromStringArray[$i],$ToStringArray[$i]);
    }
    $TargetString
  }
}

You can use this cmdlet like this:

PS > echo $env:Path | tr -FromString ";" -ToString "`n"
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\
C:\WINDOWS\System32\OpenSSH\
SATO Yusuke
  • 1,600
  • 15
  • 39
0

How about actually have Windows version of tr running on Windows? No need to learn new syntax!

Link: https://learn.microsoft.com/en-us/windows/wsl/install-win10

On Windows 10, one can enable Windows Subsystem Linux and install a light-weighted version of Ubuntu afterward.

Similar tools to WSL for Windows 7 also exists here: https://www.microsoft.com/en-us/download/details.aspx?id=2391

Pros: You get tr, bash shell and the whole arsenal of Linux command line tools.
Cons: You need to run inside the WSL bash shell to get access to the tools

biocyberman
  • 5,675
  • 8
  • 38
  • 50