I've seen different ways to search a text file for one character, but how would I count a-z and A-Z, spaces, digits, newlines, and special characters? I'm trying to do this in a bash script as well.
1 Answer
I'm not sure what you mean by "special characters", but given a specific set of characters, I would count them like this:
tr -dc 'character-set' < file | wc -c
Ignoring "special characters", the rest of your requirements can be met this way:
tr -dc 'a-zA-Z 0-9\n' < file | wc -c
which will output the file minus all characters not meeting your requirements, and then pipe that to wc -c, which counts characters. All you have to do is update the character set description so it includes whatever other characters you need.
4 Comments
user3347022
Awesome, that's what I was looking to do, I was getting close but I couldn't get there! Thanks!
user3347022
Cool cool, I'm brand new here and was looking how to do that because it very much answered my post.
user3347022
Also by special characters I just mean anything that isn't letters, lumbers, spaces or new line characters.
kojiro
@user3347022 If you read the
re_format (OS X) or regex(7) (Linux) man pages, you'll see definitions for various standard character classes including cntrl and print. Those are a good start for identifying exactly the characters you want to include/exclude.