问题
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
回答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\
回答2:
there is an ugly batch-hack:
echo "%path:;="&echo "%"
回答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.
回答4:
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\
回答5:
How about actually have Windows version of tr
running on Windows? No need to learn new syntax!
Link: https://docs.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
来源:https://stackoverflow.com/questions/29291986/what-is-the-tr-command-in-windows