Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I check how many white spaces (' ', \t) there are in the first line of a file?

share|improve this question
1  
possible duplicate of stackoverflow.com/questions/8683443/… – gogoud Dec 2 '15 at 16:07
    
@gogoud that's perfect! Thanks so much – Luigi T. Dec 2 '15 at 16:12
up vote 4 down vote accepted

A straightforward way would be to select just the first line, drop non-witespace characters from it, and count how many characters are left:

head -n 1 | tr -cd ' \t' | wc -c
share|improve this answer
awk '{print gsub("[ \t]",""); exit}' < file

Or to count any blank (horizontal spacing characters), not just space and tab:

awk '{print gsub("[[:blank:]]",""); exit}' < file
share|improve this answer

You can use GNU sed to replace the head|tr combo (here assuming POSIXLY_CORRECT is not in the environment):

sed '1s/[^ \t]//g' |wc -c

Note: sed will always print out a newline, so the count you'll will include the newline itself. How this works: In sed, you can give the s/// command an address range, which here is just the first line. Globally replace any non-whitespace with nul and output the result to wc which counts characters.

It's possible, but ugly, to figure out a pure-sed version which does the counting.

A perl version is also simple enough:

perl -lne 's/[^ \t]//g;print length($_);last'

The principle is the same. The -l option outputs a trailing newline. The -n option wraps the script in a giant while-readline loop. And last terminates that loop.

Stéphane's awk solution reminded me you can do that also with perl:

perl -lne 'print s/[[:space:]]]//g;last'

Or replace it with [[:blank:]] to include the newline and other non-printing characters.

share|improve this answer
1  
Since you're calling q anyway the address range is not necessary. – Stéphane Chazelas Dec 2 '15 at 20:36
    
Removed the q – Otheus Dec 7 '15 at 0:56
perl -nE '$n = tr/ / /; say $n; exit' file
share|improve this answer
    
Shorter one perl -nE 'say tr/ \t//; exit' – cuonglm Dec 2 '15 at 17:11

(works with most shells):

IFS=; <file \                       
read -r IFS                         # read 1 line into $IFS
set -- $IFS                         # split while eliding $IFS whitespace
echo "$((${#IFS}-$#))"              # $IFS len less $IFS field count
share|improve this answer
    
Comments are not for extended discussion; this conversation has been moved to chat. – terdon Dec 4 '15 at 8:14
    
@BinaryZebra - nah. it wasnt very good. it was too complicated. – mikeserv Dec 7 '15 at 1:10
    
@BinaryZebra - i didnt intend it to be impolite, and i sent that before i found you in chat. – mikeserv Dec 7 '15 at 8:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.