0

From another cross-platform program I would like to use the UnxUtils tr.exe program exactly as under Unix but I can't manage to get it to work, the issue being the escaping. Here's an example.

Create a file blah.txt with content

%^&<>|'`,;=()!\"\\[].*?

Then

type blah.txt | tr.exe "*" "X"

tr.exe: extra operand `blah.txt' Try `tr.exe --help' for more information.

The problem is that Windows expands * to the files in the directory. But under Unix this is no problem. No matter what I tried, I can't escape the * properly under Windows. Similarly with the quotation mark ". Even if I can figure out how to rewrite the escaping properly, it will be a mess to apply this correction to a set of characters.

I managed to get sed.exe from the UnxTools to work consistently because here I can use program files with the -f option so that I don't need to escape things.

Any ideas?

MSalters
  • 173,980
  • 10
  • 155
  • 350
llja
  • 3
  • 1

2 Answers2

1

The problem is that Windows (CMD.EXE) does not expand *. Ever. That's why escaping won't fix it. That's how dir /s *.txt can find text files in subdirectories. It wouldn't work if *.txt was expanded in the directory where you currently are.

That's why Windows tools need to handle *. Apparently tr.exe gets it wrong.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Got it. Any alternative tool with a similar behavior that reads a program file like sed? I'm dealing with newlines etc which is horrendous in sed, that's why I wanted to go for tr. – llja Aug 20 '18 at 10:45
  • 1
    See https://stackoverflow.com/questions/29291986/what-is-the-tr-command-in-windows – MSalters Aug 20 '18 at 10:48
  • Okay, doesn't work with character classes etc. though I think. – llja Aug 20 '18 at 11:42
0

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
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Wow, this works! Thanks so much. I can't upvote yet unfortunately because not enough reputation. – llja Aug 20 '18 at 22:15