Skip to main content
added 12 characters in body
Source Link
Stéphane Gimenez
  • 29.5k
  • 3
  • 79
  • 88

Using tr ard wc:

function countchar()
{
    while IFS= read -r i; do echoprintf "%s" "$i" | tr -dc "$1" | wc -c;m; done
}

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin

Using tr ard wc:

function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin

Using tr ard wc:

function countchar()
{
    while IFS= read -r i; do printf "%s" "$i" | tr -dc "$1" | wc -m; done
}

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin
added 36 characters in body
Source Link
Stéphane Gimenez
  • 29.5k
  • 3
  • 79
  • 88

Using tr ard wc:

function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}
function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin
$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin

Using tr ard wc:

function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin

Using tr ard wc:

function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin
formatting
Source Link
Stéphane Gimenez
  • 29.5k
  • 3
  • 79
  • 88

Using tr ard wc:

function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}
 

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin

Using tr ard wc:

function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}
 
$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin

Using tr ard wc:

function countchar()
{
    while read -r i; do echo "$i" | tr -dc "$1" | wc -c; done
}

Usage:

$ countchar '"' <file.txt  #returns one count per line of file.txt
1
3
0

$ countchar ')'           #will count parenthesis from stdin
$ countchar '0123456789'  #will count numbers from stdin
quoting the variable…
Source Link
Stéphane Gimenez
  • 29.5k
  • 3
  • 79
  • 88
Loading
simplification
Source Link
Stéphane Gimenez
  • 29.5k
  • 3
  • 79
  • 88
Loading
unset IFS
Source Link
Stéphane Gimenez
  • 29.5k
  • 3
  • 79
  • 88
Loading
Source Link
Stéphane Gimenez
  • 29.5k
  • 3
  • 79
  • 88
Loading