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
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
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\
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\
...
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
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\
there is an ugly batch-hack:
echo "%path:;="&echo "%"
%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 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.
`n
in the Replace –
Bedpost 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:
$env:PATH -creplace [IO.Path]::PathSeparator, [Environment]::NewLine
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")
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\
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
© 2022 - 2024 — McMap. All rights reserved.
echo "$PATH" | ...
Good luck. – Intendant