How can I check how many white spaces (' ', \t
) there are in the first line of a file?
|
|||||||||
|
A straightforward way would be to select just the first line, drop non-witespace characters from it, and count how many characters are left:
|
|||
|
Or to count any blank (horizontal spacing characters), not just space and tab:
|
|||
|
You can use GNU sed to replace the
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 It's possible, but ugly, to figure out a pure-sed version which does the counting. A perl version is also simple enough:
The principle is the same. The Stéphane's
Or replace it with |
|||||||||
|
|
|||||
|
(works with most shells):
|
|||||||||||||
|