by S4M · 12y ago · 0 comments
ls |py -l 'sum(1 for x in l)'
And also I can count the number of files whose names matche a certain extension: ls |grep .py |py -l 'sum(1 for x in l)'
I am not saying it was not possible before, but I don't know how to do it in bash.Looking forward to use that for some basic stats on file. The following should sum up the first column of a csv.
cat data.csv| py -x 'x[0]' |py -l 'sum(x for x in l)' ls | wc -l
ls | grep -c .py
(note the cat and pipe are not needed) awk -F',' '{sum+=$1}END{print sum}' data.csv py -l 'len(l)'
That being said, I still use wc -l every time. In fact, I always prefer regular unix commands (e.g. grep, xargs, head) to pythonpy when possible. But there are some things, such as: py -l 'l[::2]'
which while possible with tools like sed, are just much better expressed with pythonpy.That won't work if there are files with names like foo.py.txt.
These will:
ls | grep -c '.py$'
or better, because less names to filter out:
ls -l *.py | wc -l
In the meantime feel free to use this python thing, its rather fun to be honest. Just wanted to demonstrate the unix-y way of doing it.
To work with input split into columns, use awk. By default, it assumes the columns are separated with spaces. This can be customized with the '-F' option.
So your last example can be rewritten as
awk -F, '{s+=$1} END{print s}' data.csv ls | wc -l
ls | grep .py | wc -l