What Is The 'Tr' Command in Windows

What is the `tr` command in Windows?



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\

windows tr -s equivalent

Mmmm, I'll avoid saying Windows is rubbish, and just refer you to UnixUtils which is simpler than cygwin and doesn't need much installation.

Get it here

Windows tr output file

It looks like cmd is getting confused with "{\"". First, the backslash works correctly in escaping the quote. But two consecutive quotes are taken by cmd to mean an escaped quote. Then the rest of the line is taken as the same sentence. You can see the effect using printf:

C:\>printf "%s\n" "x\"" > nul
x"
>
nul

Thus, printf takes each word individually, but cmd sees them as all part of a quoted string, and therefore does not parse the > nul as anything other than normal words.

The solution? Use two consecutive quotes in your string:

cat file.txt | tr -d "{""" > output.txt

Redirect `tr -d` on windows?

Put redirection before options:

tr > vcs.txt -d \"

Get UnxUtils tr.exe for Windows work like the Unix tr with character escaping

Some easy portable ways to deal with the file globbing when using * (by the way, as MSalters points it is not a windows feature)

type blah.txt | tr.exe \52 X
type blah.txt | tr.exe [=*=] X

Is there a way to run a tr command (or something like it) in Windows Perl?

Break down the bits to what each does and convert that to Perl:

tr -cd '[:print:]\n\r'

Take the complement (-c) of printable characters, newlines, and carriage returns and delete them (-d). The tr manpage tells you which each of those do.

tr -s ' '

Collapse multiple same characters (-s) to one character.

sed -e 's/ $//'

Get rid of a trailing space.

Now just do that in whatever language you want to use. Putting it together, you might like something like:

perl -pe 's/\P{PosixPrint}//g; tr/ //s; s/ \z//;'

Note that Perl's tr does not do character classes, but I can use a complemented (\P{...} with the uppercase P) Unicode character class to do the same thing. Perl character classes also understand the regular POSIX character classes.

tr command not working

Converting a comment into an answer since it seems to be accurate:

What are you seeing? Could the trouble be that the file has carriage returns in it (CRLF line endings from a Windows machine)? If so, you probably see '7(outof471)' as the output.



Related Topics



Leave a reply



Submit