Bash - Same frequency


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - Same frequency
# 1  
Old 04-18-2011
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


Code:
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:

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

any help will be much appreciated

Last edited by vgersh99; 04-18-2011 at 01:10 PM.. Reason: code tags, please!
# 2  
Old 04-18-2011
Hmmm.

Code:
# 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.
# 3  
Old 04-18-2011
Try:
Code:
#!/bin/bash
f=`grep $1 name.txt | awk '{print $2}'`
awk -vf=$f '$2==f{print $1}' name.txt | sort

# 4  
Old 04-18-2011
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"
Code:
 
 
#!/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 Smilie

Last edited by jboy; 04-18-2011 at 10:11 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

frequency count using shell

Hello everyone, please consider the following lines of a matrix 59 32 59 32 59 32 59 32 59 32 59 32 59 32 60 32 60 33 60 33 60 33 60 33 60 33 60 33 60 33 60 33 60 33 (7 Replies)
Discussion started by: xshang
7 Replies

2. Shell Programming and Scripting

find frequency

Hi, I have a file with more than 1 million records.. Each row has a distance number.. I want to know how many times is each number occurring from min to max number.. 2 5 120 208 7 28 45 6 33 120 7 208 so onn.. output 0-0 (4 Replies)
Discussion started by: Diya123
4 Replies

3. Emergency UNIX and Linux Support

Different OS Kernel Update Frequency

Hi guys I'm trying to configure a collaboration suit (PHP, PostgreSQL, Apache) plus a mail server(Sendmail and Dovecot) on a single box. It will be used heavily 24*7. So having a long up time is really needed. I'm looking for 3 to 6 month up time. I want to know about kernel update... (6 Replies)
Discussion started by: majid.merkava
6 Replies

4. AIX

Finding Memory Frequency

Hi, I would like to know how to find out frequency of memory, I have used the command prtconf but it is showing amt of ram available on server, it is not showing frequency. Regards, Manoj (1 Reply)
Discussion started by: manoj.solaris
1 Replies

5. UNIX for Dummies Questions & Answers

Frequency of a range of numbers

Hello, I have a column where there are values from 1 to 150. I want to get the frequency of values in the following ranges: 1-5 6-10 11-15 .... .... .... 146-150 How can I do this in a for loop? Thanks, Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

6. Shell Programming and Scripting

Calculating cumulative frequency

Hi, I have a file containing the frequency's of an element sorted in ascending order. The file looks something like this: #Element Frequency 1 1 2 1 3 1 4 1 5 1 6 ... (5 Replies)
Discussion started by: sajal.bhatia
5 Replies

7. Shell Programming and Scripting

Sorting value frequency within an array

How is it possible to sort different nummeric values within an Array. But i don`t want the highest or the lowest. I need the most frequently occurring value. For examble: My Array has to following values = (200 404 404 500 404 404 404 200 404) The result should be 404 The values are... (3 Replies)
Discussion started by: 2retti
3 Replies

8. Shell Programming and Scripting

Counting commands frequency

I'd like to create a script that keeps track of the commands used. For example: count ls ps and from now on whenever I use ls or ps a counter is increased. I want that after the command count has been activated a session starts(here I can use any commands) and I want to stop the session... (5 Replies)
Discussion started by: Max89
5 Replies

9. Solaris

out of frequency

:cool: after installing solaris 10 5/08/09 directory the computer rebooting then the massage "out of frequency" appear.i want a solution first second my main board is GA-MA780G UD3H (14 Replies)
Discussion started by: medo2008
14 Replies
Login or Register to Ask a Question