-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
  • 66,199
  • 35
  • 114
  • 250
John B
  • 9
  • 2

4 Answers4

4
awk -v RS= '
  {word = tolower($1); n = ++count[word]}
  n > max {max_word = word; max = n}
  END {print max_word}'
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
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
Swapnil Dhule
  • 431
  • 1
  • 6
  • 13
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нιη
  • 40,939
  • 15
  • 71
  • 114
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14
-1

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

Kalpesh Bhoj
  • 192
  • 1
  • 10
  • 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`](https://unix.stackexchange.com/a/513007/72456) do the job then. – αғsнιη Apr 23 '19 at 06:03