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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to write a script to match a searched name to a given list?
# 1  
Old 10-17-2017
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:

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

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

It returns this result:
Code:
ANA
ANNETTA
RENEE

So it's close but this is what each of the lines looks like in full:
Code:
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!

Last edited by Scott; 10-17-2017 at 07:59 PM.. Reason: Please use code tags
# 2  
Old 10-17-2017
Quote:
Originally Posted by aheyhey
[...]
So it's close but this is what each of the lines looks like in full:
Code:
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.
[...]
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.
This User Gave Thanks to Aia For This Post:
# 3  
Old 10-17-2017
Ohhh I see why it's matching to Annetta now .. thanks Aia!

Quote:
Originally Posted by Aia
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.
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?
# 4  
Old 10-17-2017
Add the -F flag to grep to interpret the value as a string instead of a regex
Quote:
Originally Posted by aheyhey
Code:
#!/bin/bash
freq="$(grep "\<$1\>" filename | cut -f2 -d',')"
grep -F "$freq" filename | cut -f1 -d',' | sort

# 5  
Old 10-18-2017
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write script to scan ip list through Nessus?

Scripting language: Shell script I want to Scan IP's from IPlist.txt through Nessus using shell scripting language. Give the Input (IPlist) to nessus and generate Nessus report in xml or PDF form which is saved automatically on computer . Please help if any one has idea about how to write... (2 Replies)
Discussion started by: sk151993
2 Replies

2. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Awk; pattern match, remove and re write

the following pattern match works correctly for me awk '/name="Fruits"/{f=1;next} /"name=Vegetables"/{f=0} f' filename This works well for me. Id like to temporarily move the match out of the file ( > newfile) and be able to stick it back in the same place at a later time. Is this... (7 Replies)
Discussion started by: TY718
7 Replies

4. Shell Programming and Scripting

[Solved] Pattern match and write to separate files

I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2). Mapping for pattern match and add line : for Alpha 123 for Beta 234 for Gamma 345 no match (goes into another file) File 1 ... (3 Replies)
Discussion started by: w020637
3 Replies

5. UNIX for Dummies Questions & Answers

Match columns and write specific word

Hi all I have another question as of now. I have two files One file contain data like this Serendipity glamerus Shenpurity In another file these entries are present in different columns like this from 2 column onwards SRN Serendipity Non serendipity ... (1 Reply)
Discussion started by: Priyanka Chopra
1 Replies

6. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

7. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

8. Shell Programming and Scripting

Match username to a name in list!(help:P)

Hello, I'm taking a username from keyboard as input and i want to check if it exists in a list under a directory for example "/home/students/" .... if there is a match i want to type the list... can anyone give me an example or a tutorial to read plz? thnx for your help. (1 Reply)
Discussion started by: lupospiros
1 Replies

9. Shell Programming and Scripting

match text from two files and write to a third file

Hi all I have two files X.txt and Y.txt. Both file contains same number of sentences. The content of X.txt is The filter described above may be combined. and the content of Y.txt is The filter describ+ed above may be combin+ed. Some of the words are separated with "+"... (2 Replies)
Discussion started by: my_Perl
2 Replies

10. Shell Programming and Scripting

how to write a script contain list of questions

hi i want to write a script that contain questions upto 50..when i run the script it has to ask the questions one by one with options like a,b,c,d and user has to answer the each question and finally result (contain how many user selected correctly) please help me...i need hints to do....... (8 Replies)
Discussion started by: srinivas2828
8 Replies
Login or Register to Ask a Question