counting a particular character in a file

fold -w 1 <file> | grep -c <character>

By: KeePon
2010-09-23 16:30:33

These Might Interest You

  • grep '^[^#]' sample.conf \__/ |||| \_________/ | |||| | | |||| \- Filename | |||| | |||\- Only character in group is '#' | ||| | ||\- Negate character group (will match any cahracter *not* in the | || group) | || | |\- Start new character group (will match any character in the | | group) | | | \- Match beginning of line | \- Run grep Empty lines will also be not matched, because there has to be at least one non-hash-sign character in the line. Show Sample Output

    grep '^[^#]' squid.conf
    Flow · 2013-02-21 18:51:06 4
  • Remove CR LF (CRLF) from a text file. use man:tr to get more info about removing other characters from a file. and even character to character replacements.

    tr -d '\r\n' < input_file.txt > output_file.txt
    nadavkav · 2009-10-26 16:12:21 0
  • Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input. Show Sample Output

    ps aux | grep [a]pache2 | nl
    donnoman · 2009-12-17 18:48:09 1
  • Files saved on a windows machine use different ascii characters for lines turns. When viewing such files in VI the will most often have a ^M(control-VM) character at the end of each line. This command will remove all occurrences of that character

    roliver · 2009-02-17 01:23:39 1

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: