How to write a script to match a searched name to a given list?

Full title: How to write a script to match a searched name to a given list, and then returns other names with the same properties

Anyway, first time here, hi! So I'm taking an introductory course at uni and there's a question in our lab that asks us to write a script where a user can search a name which will then be compared to a given list of names where each line looks like this:

ANA,0.120,55.989,181

The script has to then look at the frequency associated with that name (second column, so in ANA's case, 0.120) then output a list of names which match that frequency and sort it in alphabetical order.

This is what I've got so far:

#!/bin/bash
freq="$(grep "\<$1\>" filename | cut -f2 -d',')"
grep "$freq" filename | cut -f1 -d',' | sort

It returns this result:

ANA
ANNETTA
RENEE

So it's close but this is what each of the lines looks like in full:

ANA,0.120,55.989,181
ANNETTA,0.006,83.290,1203
RENEE,0.120,56.109,182

I have no idea why it's giving me ANNETTA since it doesn't even match ANA's value of 0.120.

I'm sorry this was heaps long, but any help would be much appreciated thank you!

A . (period) will match any character except a new line by default when used as a regex.
The variable $freq will contain the expression 0.120 A character zero, followed by any character except a new line, followed by the character one, followed by the character two, followed by character zero.

grep is matching that in the ANNETTA line if you notice what I have highlighted in red.

1 Like

Ohhh I see why it's matching to Annetta now .. thanks Aia!

I'm not sure I understand ... If I specify a regular expression in the frequency variable, will the script still work if I searched for a name with a different frequency number? Like say if I searched Annetta which has 0.006.. shouldn't I give the script freedom so it can find other names with a frequency of 0.006?

Add the -F flag to grep to interpret the value as a string instead of a regex

A little late, but please for home- or classwork post in our homework-and-coursework-questions forum ONLY!

FYI, there's other, more compact solutions as well allowing to get what you need in one command.