温馨提示:本文翻译自stackoverflow.com,查看原文请点击:bash - Word length frequency count with Linux commannds
bash cygwin pipe

bash - Linux命令的字长频率计数

发布于 2020-04-18 22:55:39

我想获取单词长度的频率计数(test.txt是单词列表)。下面的代码应计算不同长度的单词数,然后将结果相加;重复直到达到文件中的单词总数。问题在于cur变量的分配,该变量应存储管道操作(grep | wc -w的输出一旦达到该语句,cur:command not found就会在终端上显示。我正在使用Cygwin。我该如何运作?

在此处输入图片说明

#!/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"

查看更多

提问者
Helen Grey
被浏览
89
25.5k 2020-02-05 04:58

不允许在作业周围留空格:

cur=$(grep...