What is the `tr` command in Windows?
Asked Answered
J

8

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
Juback answered 27/3, 2015 at 1:48 Comment(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?Triumphal
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.Intendant
M
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\
Meltwater answered 27/3, 2015 at 1:55 Comment(1)
Even simpler to type: $env:Path -split ";"Interactive
S
9

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\
...
Stinko answered 23/2, 2021 at 17:15 Comment(1)
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}}Compossible
M
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\
Meltwater answered 27/3, 2015 at 1:55 Comment(1)
Even simpler to type: $env:Path -split ";"Interactive
S
8

there is an ugly batch-hack:

echo "%path:;="&echo "%"
Skylark answered 26/9, 2015 at 17:8 Comment(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.Skylark
S
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.

Stinko answered 17/4, 2018 at 7:44 Comment(1)
According to #326453, you really only need `n in the ReplaceBedpost
B
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
Bourges answered 22/1, 2022 at 20:57 Comment(0)
C
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")
Celisse answered 2/10, 2020 at 16:13 Comment(0)
H
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\
Histopathology answered 19/6, 2019 at 15:26 Comment(0)
F
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

Feudalize answered 2/4, 2019 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.