LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2008, 10:36 PM   #1
kdyzsa
LQ Newbie
 
Registered: May 2008
Posts: 7

Rep: Reputation: 0
Bourne shell script traverse


I've only started learning UNIX 2 days ago since my supervisor wanted me to. She gave me some exercises which I successfully answered thanks to google and UNIX tutorials. But this one is taking me longer than usual so I come here to seek for help.

This is the exercise she gave me:
>> User will prompted to search for either by last name (-l) or first name(-f). The script named as searchcomp will get the exact matching parameter based from input.txt and display the breakdown of other details.
Format of execution will be like below:
searchcomp [-l] [-f] ‘pattern’
ex. searchcomp -f 'Tang'

Output will be:

First Name: <first name field>
Last Name: <last name field>
Exercise 1: <grade of exercise 1>
Exercise 2: <grade of exercise 2>
Case Study: <grade of case study>
Average: 15% of exer1 + 15% of exer2 + 70% of case study
Remark: ‘Fail’ for 74 below
‘Pass’ for 75 above
<<
Input.txt looks like this:
>>
LastName:FirstName:Exer1:Exer2:Case:
Tang:Sam:90:40:40
Dylan:Tom:80:48:50
Smith:John:34:67:89
...
<<
I don't have any problems with getting the average or whether its pass or fail. But I'm kinda stumped on how to get the firstname. My idea was to grep it then I really don't know how to seperate them since I can't put it in a seperate file. I'm nto allowed. So if the results of the grep were.
Ex.

Tang:Sam:90:40:40
Tang:Kim:94:78:40

I'm not sure how I can seperate the Tang as the lastname and Sam as a firstname? or the exercises? Is there any way to traverse the line/string? I'm also thinking if there were multiple results, I would have to seperate them as well. Am I on the right path or is my algorithm for solving this wrong? Any help would be appreciated with this, thanks
 
Old 05-05-2008, 10:46 PM   #2
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
The "cut" command is what you're looking for...

Basic example given the following data in a file "input.txt":

Tang:Sam:90:40:40
Dylan:Tom:80:48:50
Smith:John:34:67:89

Find Sam Tang's first name only:

grep Tang input.txt | cut -d':' -f2

grep pulls just the line you want, then cut is passed two parameters: -d (delimiter, which in this case is set to ':') and -f (field, which in this case is "field" #2). If I used this command on /etc/passwd in this form:

snippet from /etc/passwd:
Code:
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
operator:x:11:0:operator:/root:/bin/bash
grep root /etc/passwd | cut -d':' -f1-2 --output-delimiter=" "

I get this:

root x
operator x

Edit: You can also accomplish this with sed or awk...I'll give a basic sed example (awk would be much easier!):

sed -e 's/\([A-Za-z]*\):\([A-Za-z]*\):\([0-9]*\):\([0-9]*\):\([0-9]*\)/First Name: \1\nLast Name: \2\nExercise 1: \3\nExercise 2: \4\nCase Study: \5\n/g' input.txt

Last edited by rocket357; 05-05-2008 at 10:59 PM.
 
Old 05-06-2008, 02:18 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Awk will produce much quicker results than the alternative Shell script, especially if the input file is large.

Here is a partial awk example:

Code:
#!/bin/bash
if [ "$1" == "-l" ]
then
awk -F":" 'function REPORT() {
   print "First Name:", $2
   print "Last Name :", $1
   print "Exercise 1:", $3
   print "Exercise 2:", $4
   print "Case Study:", $5
   l_avg=(($3 * .15) + ($4 * .15) + ($5 * .7))
   print "Average   :", l_avg
}
{
   if ( $1 == lName ) REPORT()
}' lName=$2 input.txt
fi
 
Old 05-06-2008, 06:56 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Also w/o awk/grep/cut, see BaSH pattern replacement: string="Tang:Sam:90:40:40"; string=("${string//:/ }"); echo "${string[0]}".
 
Old 05-06-2008, 07:48 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
If it's a shell scripting exercise for th sake of learning, using awk is probably not the right direction, although I think it's a good choice for doing such a task "for real". Perl nice - especially using the Getopt module provides a lot of functionality with a friendly interface (plus you can document your program with POD tags).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate a random number from a bourne shell script lothario Linux - Software 2 03-01-2007 11:01 PM
for statement in bourne shell script bujecas Linux - General 3 07-17-2006 07:32 AM
file permissions in bourne shell script bujecas Linux - General 2 07-12-2006 11:46 AM
Help with basic bourne shell script rnj Programming 6 09-13-2005 08:41 PM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration