searching and mapping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching and mapping
# 1  
Old 06-19-2009
searching and mapping

Hi,
I just started looking into various aspect of unix shell scripting. I am completely new to the world of UNIX. Could any one help me in solving the following requirement.

I have a parameter file with some data e.g.
sample.param
-------------
Andrew=201
Bob=219
Shelly=239
Student_219=/dir1/stud.txt
Student_240=/dir1/stud2.txt

Now I need to write a shell script i.e. testStud.sh which will satisfy the following requirement:
1. I'll pass a string as an argument while running the script.
2. The script will search the string in sample.param file as a key and return the value for that key. if it's not present in the param file, it'll exit out of the program displaying appropriate message.
3.Now the value returned will be appended to the string "Student_" and the resultant string will be searched as akey in the param file and the value corresponding to this key will be returned.

To explain the requirement in a broader way... let's take an example. Let's say the user runs the script as ./testStud.sh Bob
Now value corresponding to the argument i.e. key "Bob" in param file is "219". Now "219" will be appended to "Student_" and the resulltant string is "Student_219". Now again this key will be searched in the param file and the final result will be the value "/dir1/stud.txt"

Again let's say some one runs the script as ./testStud.sh Roger
Now since there is no key named "Roger" in the param file, it'll exit out of the program with a message "key not found".
Could any one kindly provide me the shell script for testStud.sh?
# 2  
Old 06-19-2009
Is this for a homework assignment?
# 3  
Old 06-19-2009
What have you tried?
What issues are you facing?
# 4  
Old 06-19-2009
hi

Ya, actually it's a small part of the whole requirement.
I tried to solve it, but the output that I'm getting is not perfect. Here is the script that I've come up with:

#!/bin/sh
arg_1=$1
echo "arg passed by user is:${arg_1}"
res=$(awk -F= -v name=${arg_1} '$1 == name {value=$2;exit} END { print (value)?value:"not found"}' sample.param)
echo "mapping is $res"
if [ "${res}" = "not found" ]; then
echo "key not found"
help
exit 1
fi

key=Student_${res}
echo "key:${key}:"
param_file=`grep '${key}=' sample.param | awk -F"=" '{print $2}'`
echo "param file is:${param_file}"

---------------------------------------------------------

But the output is not coming properly especially the last result i.e. the value of "param_file" variable which is displayed as blank though the value is present in the param file.
# 5  
Old 06-19-2009
Quote:
Originally Posted by mamatabbsr
Ya, actually it's a small part of the whole requirement.
I tried to solve it, but the output that I'm getting is not perfect. Here is the script that I've come up with:

#!/bin/sh
arg_1=$1
echo "arg passed by user is:${arg_1}"
res=$(awk -F= -v name=${arg_1} '$1 == name {value=$2;exit} END { print (value)?value:"not found"}' sample.param)
echo "mapping is $res"
if [ "${res}" = "not found" ]; then
echo "key not found"
help
exit 1
fi

key=Student_${res}
echo "key:${key}:"
param_file=`grep '${key}=' sample.param | awk -F"=" '{print $2}'`
echo "param file is:${param_file}"

---------------------------------------------------------

But the output is not coming properly especially the last result i.e. the value of "param_file" variable which is displayed as blank though the value is present in the param file.
Now since you have done something, it feels good to guide you.
1) May be help you want to write later...
2) remove the quotes from '${key}=' and make it just ${key}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Many to many -- mapping

INPUT 13333--TEXT1 14444--TEXT2 13333--TEXT3 12233--TEXT5 14444--TEXT5 12233--TEXT1 12222--TEXT5 13333--TEXT09 what I'm looking for is something using awk arrays with below given output. 14444--TEXT2,TEXT5 13333--TEXT1,TEXT3,TEXT09 12233--TEXT5,TEXT1 12222--TEXT5 (6 Replies)
Discussion started by: busyboy
6 Replies

2. UNIX for Dummies Questions & Answers

Mapping drive

please forgive me. i know this is unix forum. CIFS can map to shared windows folder. i just wonder if windows can map to unix shared folder. if yes, please enlight me... (5 Replies)
Discussion started by: lawsongeek
5 Replies

3. Homework & Coursework Questions

Mount and Mapping are same???

Please let me know if mounted on in Unix and Mapping in Windows are same thing??? (1 Reply)
Discussion started by: MZC
1 Replies

4. Red Hat

drive mapping

What is the eqiuvalent of /dev/dsk/rdsk in linux vs Solaris (1 Reply)
Discussion started by: walnutpony123
1 Replies

5. Shell Programming and Scripting

Creating unique mapping from multiple mapping

Hello, I do not know if this is the right title to use. I have a large dictionary database which has the following structure: where a b c d e are in English and p q r s t are in a target language., the two separated by the delimiter =. What I am looking for is a perl script which will take... (5 Replies)
Discussion started by: gimley
5 Replies

6. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

7. Shell Programming and Scripting

Need Help Mapping Arrays

I have the following arrays with different lengths that I want to map them with the same key. # Week numbers, 8 columns @headers = ("2011-34", "2011-35", "2011-36", "2011-37", "2011-38", "2011-39", "2011-40", "2011-41"); %data = ("2011-34", BCE, "2011-35", YZA, "2011-36",... (5 Replies)
Discussion started by: tqlam
5 Replies

8. UNIX for Dummies Questions & Answers

Re-Mapping Printers.

Hi we have a situation where some printers are on a server that sometimes has to be rebooted. If this happens the Unix boxes we have that are referencing the printers in the vfstab file fail to work even when the print server is brought back up. Does anyone know if it would be possible to put... (0 Replies)
Discussion started by: Hadleyshope
0 Replies

9. Shell Programming and Scripting

Pattern mapping

Dear Friends, Please help me on this I have file A.txt containing text lines as below grectec; 30 ,50, 60, base_123 ; top09 grectec; 30 ,55, 60, base_123 ; top09 grectec; 10 ,53, 60, base_123 ; top09 grectec; 50 ,57, 60, base_123 ; top09 ... ... another file B.txt containing test... (4 Replies)
Discussion started by: Danish Shakil
4 Replies

10. UNIX for Dummies Questions & Answers

Keyboard Mapping HP-UX

This is truly embarrassing but I have to know the answer anyway :p Can anyone share, how to assign keyboard mapping in hp-ux system? Every time I issue command which contain character "@", it'll go the next line. What I need is how to assign the character "@" to @ as it is supposed to be. ... (2 Replies)
Discussion started by: Olli.Lang
2 Replies
Login or Register to Ask a Question