How to count the number of a specific character in each line?

Tim

I was wondering how to count the number of a specific character in each line by some text processing utilities?

For example, to count " in each line of the following text

"hello!" 
Thank you!

The first line has two, and the second line has 0.

Another example is to count ( in each line.

maxschlepzig

You can do it with sed and awk:

$ sed 's/[^"]//g' dat | awk '{ print length }'
2
0

Where dat is your example text, sed deletes (for each line) all non-" characters and awk prints for each line its size (i.e. length is equivalent to length($0), where $0 denotes the current line).

For another character you just have to change the sed expression. For example for ( to:

's/[^(]//g'

Update: sed is kind of overkill for the task - tr is sufficient. An equivalent solution with tr is:

$ tr -d -c '"\n' < dat | awk '{ print length; }'

Meaning that tr deletes all characters which are not (-c means complement) in the character set "\n.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to get the line number of those lines that have a specific character?

분류에서Dev

How to count the number of character in a comma separated line where commas within delimiter are not to be counted as separate?

분류에서Dev

How to count the number of apparitions of a character in a string

분류에서Dev

How to append number at the beginning of each line [PHP]?

분류에서Dev

how to replace specific character(s) in string by number(s)

분류에서Dev

How to count the number of characters in a command line argument by using and creating a function

분류에서Dev

How to count the number of parameters in a given line within a CSV file

분류에서Dev

Count occurrence times of each character in string

분류에서Dev

Count number of special character combination of delimiters

분류에서Dev

Assign number on specific line to a variable

분류에서Dev

How do I count the number of times a keyword appears for each section of a table?

분류에서Dev

Frequency of specific symbols on each line of a text file

분류에서Dev

Extract specific characters from each line

분류에서Dev

sort lines according to number of fields in each line

분류에서Dev

Number of same line in each text file

분류에서Dev

How can I count and log the number of rows in a sheet with a specific month/year value

분류에서Dev

Add and number blank line above each line in a file

분류에서Dev

cat command appears to be adding extra $ character at the end of each line

분류에서Dev

How to split a long string based on character count

분류에서Dev

How to split a long string based on character count

분류에서Dev

sed: find and replace nth character in a line that starts with a specific string

분류에서Dev

For each list, count the total number of elements with matching class

분류에서Dev

How to cut a string after a specific character in unix

분류에서Dev

Count total number of files in particular directory with specific extension

분류에서Dev

How to count each month without repeating the lines?

분류에서Dev

Vim: Replace first word on each line of selection with number in list

분류에서Dev

How to count empty line using grep

분류에서Dev

Extract a word from a line, doing this to a specific number of lines

분류에서Dev

Count and display how many of specific column result

Related 관련 기사

  1. 1

    How to get the line number of those lines that have a specific character?

  2. 2

    How to count the number of character in a comma separated line where commas within delimiter are not to be counted as separate?

  3. 3

    How to count the number of apparitions of a character in a string

  4. 4

    How to append number at the beginning of each line [PHP]?

  5. 5

    how to replace specific character(s) in string by number(s)

  6. 6

    How to count the number of characters in a command line argument by using and creating a function

  7. 7

    How to count the number of parameters in a given line within a CSV file

  8. 8

    Count occurrence times of each character in string

  9. 9

    Count number of special character combination of delimiters

  10. 10

    Assign number on specific line to a variable

  11. 11

    How do I count the number of times a keyword appears for each section of a table?

  12. 12

    Frequency of specific symbols on each line of a text file

  13. 13

    Extract specific characters from each line

  14. 14

    sort lines according to number of fields in each line

  15. 15

    Number of same line in each text file

  16. 16

    How can I count and log the number of rows in a sheet with a specific month/year value

  17. 17

    Add and number blank line above each line in a file

  18. 18

    cat command appears to be adding extra $ character at the end of each line

  19. 19

    How to split a long string based on character count

  20. 20

    How to split a long string based on character count

  21. 21

    sed: find and replace nth character in a line that starts with a specific string

  22. 22

    For each list, count the total number of elements with matching class

  23. 23

    How to cut a string after a specific character in unix

  24. 24

    Count total number of files in particular directory with specific extension

  25. 25

    How to count each month without repeating the lines?

  26. 26

    Vim: Replace first word on each line of selection with number in list

  27. 27

    How to count empty line using grep

  28. 28

    Extract a word from a line, doing this to a specific number of lines

  29. 29

    Count and display how many of specific column result

뜨겁다태그

보관