UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old October 13th, 2004, 04:43 PM
yogi27 yogi27 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Dallas
Posts: 50 yogi27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Help needed getting particular data from a table

Hi,

I have some data in a tabular format. For example:

Name Id Class Grade GPA
A 21 X A 4.0
B 55 IX C 3.8
C 78 X A 3.9

What command or code would help me get the GPA for a particular Class and Grade. I am working on korn shell scripting.

Thanks.

-Yogesh.

Reply With Quote
  #2  
Old October 13th, 2004, 04:44 PM
yogi27 yogi27 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Dallas
Posts: 50 yogi27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Sorry, the table hasn't come properly.

Reply With Quote
  #3  
Old October 14th, 2004, 09:49 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
Code:
#!/bin/ksh
# file = z.sh 
# read data file, do floating point average
classcode="X"
average="0"
let cnt=0
while read Name Id Class Grade GPA
do
       if [ "$Class" = "$classcode" ]; then
          average=`echo "$average + $GPA" | bc` 
          let cnt=$cnt+1
       fi
done < data
average=`echo "scale=3; $average / $cnt" | bc`
echo "Average = $average"
exit 0

Reply With Quote
  #4  
Old October 14th, 2004, 10:33 AM
yogi27 yogi27 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Dallas
Posts: 50 yogi27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Hi Jim,

I do not need the Average. I tried this: cat <filename> | grep X

But I get both 'X' and 'IX'. Is there a way to specify that I need only 'X'.

After I get the specific line I wanted, how do I get the last field i.e. GPA.

Thanks,
Yogesh.

Reply With Quote
  #5  
Old October 15th, 2004, 01:51 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
did you already discover the 'man' command ?

let things like: cat <filename> | grep X
for dos-experts, use unix cmd:
grep -w X filename
or
sed -n '/\<X\>/p' filename
sed is my preferred, better controllable.

Reply With Quote
  #6  
Old October 15th, 2004, 10:39 AM
yogi27 yogi27 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Dallas
Posts: 50 yogi27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
HI guggach,

I had already tried the man pages but it never gave me the -w option. It gave me this:
-x (eXact) Matches are recognized only when the entire input line matches the fixed string or regular expression.

Anyways, this is a small section of my table.

0x0024 2 0x0000 00 009 00005 0x2002
0x0023 2 0x0000 00 032 00000 0x2001
0x0023 3 0x0000 00 000 00100 0x2000

I do a grep -w 2 | grep 0005 <filename> to get:
0x0024 2 0x0000 00 009 00005 0x2002

But I am interested in the last field 0x2002. How do I get this in a sible grep command? Thanks.

-Yogesh.

Reply With Quote
  #7  
Old October 15th, 2004, 11:18 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
-w for a good grep is: word matching, try the sed
command, it's anyway the better one.
wich OS are you on ?

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Help needed getting particular data from a table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway