I've to extract the number of words using each letter of the alphabet from A to Z. The script that i've to create create take as parameter the name of the dictionary file to analyze and will be used like this ./langstat.sh dico.txt

I tried few things like this :

    cat ~/dico.txt | grep -o A | wc -l

The problem is that it gives me the number of A into all the file, but I want to know only the number of word having a A into.

Could you give me a hand please?

  • grep -ci A ~/dico.txt. See also: fold -w1 ~/dico.txt | sort | uniq -c – Stéphane Chazelas Feb 24 '15 at 15:47

You could use this instead:

for c in {A..Z}; do
  echo -n "$c: "
  tr ' ' '\n' < dico.txt | grep $c | wc -l

It works as follows: The for loop runs through each character from A to Z. tr replaces all spaces with newlines, so every word has its own line. Then grep searches for the character in the word an prints it if the character is found. wc counts those lines.

  • I would like to sort those informations, for exemple there are more K than X so i would like to see C:38456 E:36756 ... K:456 X:326. – Julien S Feb 25 '15 at 9:19
  • @JulienS add | sort -rk2 after done. It sort the second field descending. – chaos Feb 25 '15 at 9:24
  • That works for me: for c in {A..Z}; do echo -n "$c: "; tr ' ' '\n' < dico.txt | grep $c | wc -l; done | sort -rk2 – chaos Feb 25 '15 at 9:33

Is it helps?

tr ' ' '\n' < "$1" | sort -u > my.tmp
for letter in {A..Z}
    printf "Words with %c : " $letter
    grep -iwc "\w*$letter\w*" my.tmp
rm my.tmp
  • Thank's a lot ! I'm a rookie on Unix and this kind of fast answer saves me a lot resarches. I can continue my tutorial thank's to you ! – Julien S Feb 24 '15 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.