2

I was having trouble piping the results of a 'find' to sed. I simplified it to the simplest thing I could break, and I got this:

echo 1234567890abcdefghijklmnopqrstuvwxyz | sed 's/[:digit:]*/X/g'

I expected to get:

Xabcdefghijklmnopqrstuvwxyz

The output I get from this is:

X1X2X3X4X5X6X7X8X9X0XaXbXcXeXfXhXjXkXlXmXnXoXpXqXrXsXuXvXwXxXyXzX

which is not what I was expecting. If I change my regex to:

echo 1234567890abcdefghijklmnopqrstuvwxyz | sed 's/[0-9]*/X/g'

I get:

XaXbXcXdXeXfXgXhXiXjXkXlXmXnXoXpXqXrXsXtXuXvXwXxXyXzX

which is closer to what I expected. I just realized I don't have this problem in a standard terminal, only in aquamacs eshell... I assume it must be a character encoding issue? Maybe unicode related? How do I determine this for sure, and how do I fix this problem?

Jonathan Adelson
  • 3,275
  • 5
  • 29
  • 39
  • What do you expect exactly instead of your second example `XaXbXcXdXeXf...` ? I also use Aquamacs, and get the same output whether in Eshell or Terminal. –  Nov 20 '11 at 20:53

3 Answers3

2

Remember that the reg-exp char '*' means 'match zero or more of the previous char' ( char class in this case)

And as @SamHoice noted, you need '[[:digit:]]'.

So you can either reduced all digits in a row 1 X

echo 1234567890abcdefghijklmnopqrstuvwxyz | sed 's/[[:digit:]][[:digit:]]*/X/g'
Xabcdefghijklmnopqrstuvwxyz

Or substitute X for all digits

echo 1234567890abcdefghijklmnopqrstuvwxyz | sed 's/[[:digit:]]/X/g'
XXXXXXXXXXabcdefghijklmnopqrstuvwxyz

If neither of these work, please edit your question to include what you need as your output.

I hope this helps.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • I added what I expected to get. – Jonathan Adelson Nov 21 '11 at 01:05
  • does this not work in your eshell? Arg. didn't notice that you answered your own question. Does part 2 help? .Good luck. – shellter Nov 21 '11 at 01:08
  • Ok. I understand now. I'm not sure why it didn't seem to be working the same in term and eshell earlier. Because I left out the first [[:digit:]] I never got all of the #s substituted to one X without all the 0 Xs between the letters. – Jonathan Adelson Nov 21 '11 at 01:13
  • With cutting and pasting into a term you can sometime wind up with spaces, tabs, other invisible chars that throw off the sensitive nature of sed reg-ex ;-). That's my best guess. If you still have the failing commands in a history buffer, maybe `echo ... | od -m x-ansi` can help you debug it. Thanks for accepting and upvote! Good luck. – shellter Nov 21 '11 at 01:18
1

Answer for number one is that the character class [:digit:] itself needs to be enclosed in [], so it should be [[:digit:]].

Still working on the second part.

Jonathan Adelson
  • 3,275
  • 5
  • 29
  • 39
0
echo 1234567890abcdefghijklmnopqrstuvwxyz | sed 's/[0-9]\+\(.*\)/X\1/g'

[0-9]+ match the 1234567890

\1 is the .* any letter, except the [:digit:]

fhefh
  • 1
  • 2