Get unique string occurrence and display it

Viewed 59

I'm not really good with awk, so here what I just did to count the number occurence in one row:

the input.txt has this:

18 18 21 21 21 21 18 21

I just want to display the unique number that occur above. So, here is my code:

input="input.txt"

output=$(fmt -1 "$input" | sort | uniq | awk '{printf $1","}')

echo "$output"

The output:

18,21,

I got the result correctly but that comma , at the end, how do I remove that comma? Also, is there a simpler or a clean method without using fmt ?

The expected output:

18,21

Edit to remove comma, I use this:

sed 's/,$//'

and it's working, but is there a simpler way to do this without using fmt ?

3 Answers

Could you please try following.

awk '
BEGIN{ OFS="," }
{
  for(i=1;i<=NF;i++){
    if(!arr[$i]++){
      val=(val?val OFS:"")$i
    }
  }
  print val
  val=""
}' Input_file

Explanation: Adding detailed explanation for above.

awk '                          ##Starting awk program from here.
BEGIN{ OFS="," }               ##Setting output field separator as comma here.
{
  for(i=1;i<=NF;i++){          ##Traversing through all fields of currnet line here.
    if(!arr[$i]++){            ##Checking condition if arr is NOT having current field present in it
      val=(val?val OFS:"")$i   ##Creating val and keep adding values to it, to print at last all values.
    }
  }
  print val                    ##printing val here.
  val=""                       ##Nullify val here.
}' Input_file                  ##mentioning Input_file name here.

Here is an alternative way in gnu awk:

awk -v RS='[[:blank:]]+' '!seen[$1]++{s=s (s!=""?",":"") $1} END{print s}' file.txt
18,21
Related