|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
Sorry, the table hasn't come properly.
|
|
#3
|
|||
|
|||
|
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
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
-w for a good grep is: word matching, try the sed
command, it's anyway the better one. wich OS are you on ? |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Help needed getting particular data from a table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|