The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: write a script & sum contents!please help
dcristo
Newbie
Rank: 1



UID 109
Digest Posts 0
Credits 0
Posts 36
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:22 PM  Profile | P.M. 
write a script & sum contents!please help



write a script & sum contents!please help




hello everyone!!

i have to write a script in unix that reads a file which is in the format:
1%blabla%30
2%blabla2%50
1%blabla3%40
2%blabla4%10

and produce the total quantities of 1 and 2: "1 = 70 2 = 60"

i am taught basic unix commands, so i must keep it simple, how can i do this using loops? (i am not supposed to use functions)

i suppose that i should extract the 1st and 2nd column from the file, but after that i don't know what to do!!

any help would be appreciated!!!! thank youuuuuuuu!


Top
Els1
Newbie
Rank: 1



UID 135
Digest Posts 0
Credits 0
Posts 50
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:22 PM  Profile | P.M. 
can you show what you tried?
Top
egmeric
Newbie
Rank: 1



UID 131
Digest Posts 0
Credits 0
Posts 20
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:22 PM  Profile | P.M. 
quote:
originally posted by questionmaker25
(i am not supposed to use functions)


somebody will come and question you because it seems like a homework qns, but nevertheless,

code:
Top
bochgoch
Newbie
Rank: 1



UID 73
Digest Posts 0
Credits 0
Posts 56
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:23 PM  Profile | P.M. 
although this sounds like a homework question here is one answer. i am sure you will get more compact answers from those more accustom to awk.

create enttab to contain

1%blabla%30
2%blabla2%50
1%blabla3%40
2%blabla4%10


for name in `cat enttab`
do
echo $name
frst=`echo $name | cut -f 1 -d %`
scnd=`echo $name | cut -f 3 -d %`
echo $frst $scnd
Top
hans
Newbie
Rank: 1



UID 171
Digest Posts 0
Credits 0
Posts 43
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:23 PM  Profile | P.M. 
the only thing i can do is cut -f 1,3 -d '%' {filename}
i am a bit lost after that & i am certainly not bored to solve it, i just don't know how to do it!!
Top
forumrating
Newbie
Rank: 1



UID 149
Digest Posts 0
Credits 0
Posts 113
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:23 PM  Profile | P.M. 
quote:
originally posted by johnf
although this sounds like a homework question here is one answer. i am sure you will get more compact answers from those more accustom to awk.

create enttab to contain

1%blabla%30
2%blabla2%50
1%blabla3%40
2%blabla4%10


for name in `cat enttab`
do
echo $name
frst=`echo $name | cut -f 1 -d %`
scnd=`echo $name | cut -f 3 -d %`
echo $frst $scnd
done



i am not familiar to awk anyway!! thanks a lot but i know how to extract the columns. i don't know how to sum the contents of the thrird one!


last edited by questionmaker25 : 2 weeks ago at 05:52 am.
Top
MassBile
Newbie
Rank: 1



UID 253
Digest Posts 0
Credits 0
Posts 20
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:23 PM  Profile | P.M. 
ofcourse it is a homework!!!!
is that so bad to ask for directions? i don't want you to solve the exercise!
i just want you to tell my how to add the numbers of the third column


thank you in advance!
Top
htmlindex
Newbie
Rank: 1



UID 178
Digest Posts 0
Credits 0
Posts 22
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 19, 2007 01:24 PM  Profile | P.M. 
quote:
originally posted by questionmaker25
ofcourse it is a homework!!!!
is that so bad to ask for directions?



yes, asking for help with homework questions is a no-no as per the forum rules.


quote:
originally posted by questionmaker25
i don't want you to solve the exercise!
i just want you to tell my how to add the numbers of the third column



if i tell you how to add numbers of the third column, isn't that the same as solving your exercise?

i will give you a couple of hints: read in the file line by line, get the third column using 'cut', use shell builtin addition (check the man page of your shell) to add the number of the third column.

and i am locking this thread. no more homework posts please.
Top
 

 

All times are GMT, the time now is Jul 31, 2010 03:26 AM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.005924 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP