0

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
  • 1
    What's a "special character"? Commented Feb 24, 2014 at 14:25

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, that's what I was looking to do, I was getting close but I couldn't get there! Thanks!
Cool cool, I'm brand new here and was looking how to do that because it very much answered my post.
Also by special characters I just mean anything that isn't letters, lumbers, spaces or new line characters.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.