Skip to main content
tr solution
Source Link
maxschlepzig
  • 59.7k
  • 53
  • 224
  • 298

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.

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'

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.

re comment
Source Link
maxschlepzig
  • 59.7k
  • 53
  • 224
  • 298

You can do it with sed and awk:

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

Where dat is your example text, sed deletes (for each line) all non-" characters and awk prints for each line theits size of the first field (i.e. length is equivalent to length($0), where $0 denotes the wholecurrent line in this case).

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

's/[^(]//g'

You can do it with sed and awk:

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

Where dat is your example text, sed deletes all non-" characters and awk prints for each line the size of the first field (i.e. the whole line in this case).

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

's/[^(]//g'

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'
Source Link
maxschlepzig
  • 59.7k
  • 53
  • 224
  • 298

You can do it with sed and awk:

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

Where dat is your example text, sed deletes all non-" characters and awk prints for each line the size of the first field (i.e. the whole line in this case).

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

's/[^(]//g'