0

I have a script that works to grab each line from files and lists the count, but what I also want is to grab only the unique instances from column 3. For example the lines say things like in the pic below, and while my current script counts all instances of COMM_CONT, that I really want is for it to count the unique lines that feature COMM_CONT, which for the pic would be 2.

enter image description here

So what I want it to have the below script count the unique number of the times the four patterns happen. I feel like it should be an easy fix, but I keep trying the -u and -uniq and I can't seem to get it to do it.

#!/bin/bash
ORAL_MICRO=$(awk -F ',' '/ORAL_MICRO/{print $3;}' $1 | wc -l)
LAB_CONT=$(awk -F ',' '/LAB_CONT/{print $3;}' $1 | wc -l)
COMM_CONT=$(awk -F ',' '/COMM_CONT/{print $3;}' $1 | wc -l)
IMMUNE=$(awk -F ',' '/IMMUNE/{print $3;}' $1 | wc -l)
echo $1, $ORAL_MICRO, $LAB_CONT, $COMM_CONT, $IMMUNE >> $2
1
  • 1
    Welcome to Unix & Linux! Please don't post images of text. Instead, copy/paste the text into your question and use the formatting tools to format it as code. That way, we can actually use the data you show us to test our answers.
    – terdon
    Commented Mar 12, 2021 at 19:00

1 Answer 1

2

Two ways spring to mind:

  1. use sort to get the unique lines:

    COMM_CONT=$(awk -F ',' '/COMM_CONT/{print $3;}' $1 | sort -u | wc -l)
    
  2. do the work in awk

    COMM_CONT=$(awk -F, '
        $3 ~ /COMM_CONT/ && !($3 in seen) {seen[$3]=1; count++}
        END {print count}
    ' $1)
    
1
  • Thank you! This worked a treat!
    – Shem
    Commented Mar 15, 2021 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .