Using tr with a built in argument set
質問
I have a variable called charAlpha
. It's purpose is to go through a text document and count alphabetic characters. I am trying to use tr
and the built in [:alpha:]
set to get just alphabetic characters. Then I am piping it through wc -c
to count and get a number that is stored in charAlpha
and later in the script it will echo the number stored in charAlpha
. I hope I don't seem like I'm over explaining as I am trying to provide all information needed here. I have done that in the past and I am trying very hard not to do that! Thanks for any help!
I am using bash and editing my script within vim.
I set
charAlpha=`tr -dc [:alpha:] < | wc -c`
Then I get these two errors:
./filestats: command substitution: line 7: syntax error near unexpected token `|'
./filestats: command substitution: line 7: `tr -dc [:alpha:] < | wc -c'
解決
The <
in your pre-piped command isn't going to give tr
any input. Hence, it's erroring on the pipe (it expects a source of input).
他のヒント
The < |
part is probably your problem, you need a filename between them. Something like (it's normally recommended to use $(...)
instead of`...`
as well):
charAlpha=$(tr -dc [:alpha:] < $myFile | wc -c)