Update:
The question has changed significantly. Before responding to this, the input file should look like this:
apple apple banana orange apple orange banana orange apple ...
However, the solution will work anyway, but it can be a little complicated for this special use case.
The following awk script will complete the task:
awk '{i=1;while(i <= NF){a[$(i++)]++}}END{for(i in a){if(a[i]>1){print i,a[i]}}}' your.file
Output:
apple 3 orange 2
This is more understandable in this form:
#!/usr/bin/awk { i=1;
Then make an executable file and run it, passing it the name of the input file:
chmod +x script.awk ./script.awk your.file
source share