Count chars in lines

Counting chars in first n lines

I was coding in one of my projects and wanted to make sure that the position I was finding in a file was correct. In a first attempt, I tried copy-pasting the content of the file up to the position I was searching for into Microsoft Excel™ to find the length of the resulting string. Turns out a naive copy-paste does not preserve whitespace.

I then thought of writing a small application to do what I wanted: open a file, read the n first lines and find the total number of characters (bad reflex, I know). In trying to figure out the command line interface for this, I realized first that it would be a lot of work for something that I might not use often and second, that there might be an easier way on a *Nix system.

This lead me to simply using the applications head and wc as follows:

head -n <line_count> | wc -m