Ads

how to count words in a file?

using terminal or bash script

%%bash

wc -W ~/Work/linux.txt

20 /home/george/Work/linux.txt

using Python 3

%%python3

from pathlib import Path

file = '/home/george/Work/linux.txt'

count = Path(file).read_text().split()

print(len(count), file)

If you want to get new posts, make sure to subscribe to Value In Brief by Email.