2

Writting a script I wanted to replace all characters except alpha ([a-zA-Z]) with a line break. By definition, when using -c flag (set complement) tr is supposed to replace all characters that are not in the SET1 with the character specified in SET2. Nevertheless, it doesn't work as expected... Try this:

echo '[[:lower:]]' | tr -c '[a-zA-Z]' '\n'

Output I get:

[[
lower
]]

As it can be seen it replaces columns but not square brackets, which it it is supposed to replace.

Can anyone clarify this?

3

tr doesn't intepret character classes in that regex style.

[a-zA-Z] is interpreted as a set of [, the range of characters from a to z and ].

Therefore its complement is the set of characters including all the characters but [, the range of characters from a to z and ].

That's why [ and ] aren't replaced.

To replace any non-alphabetic character, just specify a-zA-Z as the complement of the set to be replaced:

echo '[[:lower:]]' | tr -c 'a-zA-Z' '\n'
% echo '[[:lower:]]' | tr -c 'a-zA-Z' '\n'



lower



%
  • incosistency everywhere :3 Thanks mate! – vanjadjurdjevic Nov 2 '15 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.