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
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
awk -v RS= '
{word = tolower($1); n = ++count[word]}
n > max {max_word = word; max = n}
END {print max_word}'
Below command will give you the required most repeated word along with the count.
cut -d ' ' -f1 file.txt | sort | uniq -c | head -1
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
Why not easy part... awk '{ print $1 }' myfile |uniq -c
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
word = tolower($1)
toword = $1
. – G-Man Says 'Reinstate Monica' Apr 23 '19 at 04:00