I have a file containing a long comma-delimited list of numbers, like this:
2,8,42,75,101
What's the simplest command (from a Unix shell) to get the count of numbers in this file? In the example above, that would be 5
.
I have a file containing a long comma-delimited list of numbers, like this:
2,8,42,75,101
What's the simplest command (from a Unix shell) to get the count of numbers in this file? In the example above, that would be 5
.
Easiest is probably to just count the commas:
Normally you'd add one to get the number of elements, but if there's a newline there it's counted too. Convenient in this case, I guess.
Also with awk:
as per here: http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/
this isnt too difficult
if you can define your list of elements in an array like so:
then its as simple as:
however you have a csv so this may be better: http://www.thelinuxblog.com/working-with-csv-files-in-bash/
Found this solution:
If you wanted to use python you could do:
Note that this will return the correct length regardless if there is a hanging comma.
I'm sure there is something similar and more sysadmin-y in perl but I don't use perl.
Or use grep
Actually to get this correct you would need to add one to the result