Warm tip: This article is reproduced from stackoverflow.com, please click
bash cygwin pipe

Word length frequency count with Linux commannds

发布于 2020-04-18 10:18:24

I want to get the frequency count for word lengths (test.txt is a list of words). The code below should calculate the number of words of different lengths then add the result; repeat till it reaches the total number of words in a file. The problem is with the assignment of cur variable, which should store the output of pipe operation (grep | wc -w). Once this statement is reached cur:command not found appears on a terminal. I am using Cygwin. How can I make it work?

enter image description here

#!/bin/bash

a=($(wc test.txt))
filename=$dir/freq.txt
touch $filename

words=${a[1]} #words variable stores the total number of words in test.txt file
total=0 
count=1 #first check the number of words of length 1

while [ "$total" -ne "$words" ] #terminate once total equals words
do
    cur = $(grep -o -w "\w\{$count\}" male-first.txt | wc -w) #number of words of length count
    echo "$count: $cur"
    let count++ #increment count
    let total=total+cur #add number of words of length count to total
done

echo "Done"
Questioner
Helen Grey
Viewed
62
25.5k 2020-02-05 04:58

Don't allow space around assignments:

cur=$(grep...