Ubuntu – Search for duplicates in the same line

bashcommand linescripts

I like what the uniq command does, but it looks for duplicates on different lines. I would like to find duplicates even within the same line. what command can do that?

Consider this line this this line, and that I might want to know how many times "this" appears in the same line.

Is there a command that can do this?

Best Answer

An other way using awk:

echo "this  line this this line"| \
awk  'BEGIN{print "count", "lineNum"}{print gsub(/\<this\>/,"") "\t" NR}'

count lineNum
3   1
  • Which prints count and line number in which this word found.

  • gsub() function's return value is number of substitution made. So we use that to print the number.

  • NR holds the line number so we use it to print the line number.