UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file
sample file

hi how are you
hi are you ok

sample out put

hi 1
how 1
are 1
you 1
hi 1
are 1
you 1
ok 1
wc -l filename

is not helping , i think we will have to split the lines and count and then print and also after getting sample output what i will need to do to get below out put if required

hi 2
how 1
are 2
you 2
ok 1

looking forward to hear from you

Hi,
if you have only one word by line, you could :

sort -f file | uniq -ic

Regards.

With the input sample and disedorgue's proposal, try

tr ' ' '\n' <file | sort -f | uniq -ic
      2 are
      2 hi
      1 how
      1 ok
      2 you

Hello mirwasim,

Could you please try following and let me know if this helps you(If you are not worried about the sequence of your strings).

awk '{++A[$0]} END{for(i in A){print i, A}}' RS='[ |\n]'   Input_file

Output will be as follows.

are 2
hi 2
how 1
ok 1
you 2
 

Thanks,
R. Singh

Hi.

Another, similar approach:

egrep -o '\w+' data1|sort|uniq -c

producing:

      2 are
      2 hi
      1 how
      1 ok
      2 you

On a system:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
egrep grep (GNU grep) 2.20
sort (GNU coreutils) 8.23
uniq (GNU coreutils) 8.23

See man pages for details.

Best wishes ... cheers, drl