How can I use the UNIX shell to count the number of times a letter appears in a text file?

后端 未结 10 548
清酒与你
清酒与你 2021-02-03 19:07

I have a few text files and I\'d like to count how many times a letter appears in each?

Specifically, I\'d like to use the UNIX shell to do this, in the form of: cat fil

相关标签:
10条回答
  • 2021-02-03 19:30
    grep char -o filename | wc -l
    
    0 讨论(0)
  • 2021-02-03 19:32

    try it with

    grep  [PATTERN] -o [FILE] | wc -l
    

    and please do not use cat if not needed.

    0 讨论(0)
  • 2021-02-03 19:41

    echo "a/b/c/d/e/f/g" | awk -F"/" '{print NF}'

    this will give the number of occurrence of character "/"

    0 讨论(0)
  • 2021-02-03 19:42

    In this case, i'am counting the character "|":

    expr `wc -c < filename` \- `tr -d \| < filename | wc -c`
    
    0 讨论(0)
  • 2021-02-03 19:43

    You can try easily: grep -c 'YOUR LETTER' YOUR FILE

    0 讨论(0)
  • 2021-02-03 19:45

    Another alternative:

    tr -d -C X <infile | wc -c
    

    where X is the character or string of characters you want to count and infile is the input file.

    0 讨论(0)
提交回复
热议问题