sean an
sean an

Reputation: 135

In Unix, how could I count lines of code not starting with white space?

test1:
  1. samplinggggggg
  2. samplinggggggg
  3. samplinggggggg

test2:
  1. samplinggggggg
  2. samplinggggggg
  3. samplinggggggg
  4. samplinggggggg

In above case, the number of lines not starting with white space should be 2. How could I achieve this in Unix using wc -l or better ways?

Upvotes: 0

Views: 1244

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74695

I would suggest using grep with the -c switch, to output a count:

grep -c '^[^ ]' file

The pattern matches a non-space character at the start of the line.

To include other types of white space characters (e.g. tabs), use [^[:space:]] instead:

grep -c '^[^[:space:]]' file

...or if your version of grep supports it (e.g. GNU grep), use \S instead, as a shorthand for [^[:space:]]:

grep -c '^\S' file

Upvotes: 5

Related Questions