-1

I have a paragraph and I want to know which word appears the most at the beginning of a line from all paragraph

for example: paragraph:

Hello my name is X

Nice to meet you

Hello my name is Y

so Hello appears 2 times so i will output hello

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
John B
  • 9

4 Answers4

4
awk -v RS= '
  {word = tolower($1); n = ++count[word]}
  n > max {max_word = word; max = n}
  END {print max_word}'
1

Below command will give you the required most repeated word along with the count.

cut -d ' ' -f1 file.txt | sort | uniq -c | head -1
0

Tried with below associate array method

awk 'NF{a[$1]++}END{for(x in a){print x" appears "a[x]}}' | sort -k3 -nr | sed -n '1p'

output:

Hello appears 2
αғsнιη
  • 41,407
-1

Why not easy part... awk '{ print $1 }' myfile |uniq -c

  • maybe you needed awk 'NF{ … }' infile| sort| uniq -c | sort -r|head -n1 | ... '? what you currently answered doesn't do anyting just adding 1 in front of each first word in a line. also single awk do the job then. – αғsнιη Apr 23 '19 at 06:03