Error TR command width :punct: param
문제
I have the following command:
cat original.txt | tr [:upper:] [:lower:] | tr -d [:digit:] |
tr -d [:punct:] > preproces.txt
And the system returns me the following error.
when translating, the only character classes that may appear in replaceString are 'upper' and 'lower'
The problem is giving:
tr -d [:punct:]
What's the solution to this problem?
I have searched Google and StackOverflow and don't find an answer to this problem.
Tiny edit (but significant for cut&pasters): Changed '--d' to '-d' before [:punct:] the '--d' was being rendered as '\342\200\223d' and the rendered difference was nearly invisible to the eye.
해결책
Try the following command :
tr '[:upper:]' '[:lower:]' < original.txt |
tr -d '[:digit:]' |
tr –d '[:punct:]' > preproces.txt
This avoid the infamous useless use of cat :
다른 팁
You only get the message you said you're seeing when you try something like:
tr '[:alpha:]' '[:digit:]'
I was able to use
tr -d '[:punct:]'
to delete punctuation without a problem. For example:
$ echo "tr '[:alpha:]' [:digit:] <chkit.sh" | tr -d '[:punct:]'
tr alpha digit chkitsh
$
This was on RHEL 5 for x86/64 (tr
version 'tr (GNU coreutils) 5.97').
You might note I enclosed the arguments to tr
in quotes (single or double would do, though single are generally safer). If you have any single-letter file names lurking in your directory, you could run into problems with the shell interpreting square brackets as character classes, and passing those names to tr
. Say you had a file p
:
tr [:upper:] [:lower:]
That command is now tr p [:lower:]
. The error you get is slightly different, but it might be that your problem is related to that. Be very careful with shell metacharacters; quotes are your friend.
Note that doubled square brackets are not part of the solution:
$ echo "tr -d '[[:alpha:]]' <chkit.sh" | tr -d '[[:alpha:]]'
- '::' <.
$
If your tr
does not support the [:digit:]
and [:punct:]
character classes, you will have to specify them explicitly.
tr '[:upper:]' '[:lower:]' < original.txt |
tr -d '0-9!"#$%&'"'"'()*+,./:;<=>?@[\\]^_`{|}~-' > preproces.txt
(The refactoring to merge the two tr -d
commands into one could have been done by tr -d '[:digit:][:punct:]'
in the original command, too.)
The definition of [:punct:]
was obtained from http://www.regular-expressions.info/posixbrackets.html -- I have not attempted any verification, though I refactored it slightly to make it compatible with tr
and the shell context it occurs in here.