Bash - Same frequency

Hi,

Could anyone help me with the following question, if I have two colums (names and frequency) as follows in a file called name.txt

Michael 1
Jones 1
Ben 2
Rebeca 4
David 1

and I want to use bash script called freqnames.sh that takes one argument (name) and the output should be the ALPHABETICALLY ORDERED list of all name's with the same frequency (popularity) as measured in the second column of the file.
For example.

./name.txt David

will return a list of three names (on separate lines) David, Jones, Michael,which have frequency 1.

Here is what I have tried so far but I don't know what else to add:

 
 
#!/bin/bash
a = grep -w $1  name.txt

any help will be much appreciated

Hmmm.

# Throw away the name into G, read the frequency into FREQ
read G FREQ <<<$(grep "^${1}[ \t]" filename)

grep "[ \t]${FREQ}\$" filename | sort

The <<< is a BASH syntax. For other shells you may need a temporary file.

Try:

#!/bin/bash
f=`grep $1 name.txt | awk '{print $2}'`
awk -vf=$f '$2==f{print $1}' name.txt | sort

Thanks guys, But I would like to do it with grep only no awk. I tried another code shown below and I got an error saying that " line 2: a: command not found"

 
 
#!/bin/bash
a = `grep -q  -w $1 names.txt | cut -f2 
b = `grep -w  $a names.txt | cut -f1
echo $b

Any ideas how to make this work? thanks.

---------- Post updated at 11:35 AM ---------- Previous update was at 10:49 AM ----------

Thanks guys, I got it solved.. There should be no space on the sides of the "=" sign :slight_smile: