write a script & sum contents!PLEASE HELP


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers write a script & sum contents!PLEASE HELP
# 1  
Old 05-24-2007
Data 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! Smilie
# 2  
Old 05-24-2007
Can you show what you tried?
# 3  
Old 05-24-2007
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:
awk 'BEGIN{FS="%"} { arr[$1]+=$3} END{ for(i in arr) print i " " arr[i]}' "file"

# 4  
Old 05-24-2007
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
# 5  
Old 05-24-2007
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!!
# 6  
Old 05-24-2007
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; 05-24-2007 at 07:52 AM..
# 7  
Old 05-24-2007
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! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Need to write a script user.sh that parses name & password

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a script called user.sh that parses the name and password of given in a file (user.txt) and creates the user... (1 Reply)
Discussion started by: Nygenesis
1 Replies

2. UNIX for Dummies Questions & Answers

Write terminal contents into a one file in UNIX

Hi guys, How to write terminal contents into a file in Unix operating system Actually I created GUI by using Gtk2-perl. I want to display data on GUI whatever the contents writing on terminal. So which command I have to use and where that command to be run I mean in shell script or Perl... (2 Replies)
Discussion started by: kiran425
2 Replies

3. Shell Programming and Scripting

For Loop & SUM

pcmpath query device |awk 'BEGIN{print "DEVICE NAME\tSERIAL"} /DEVICE NAME/ {printf "%s\t", $5; getline; print substr($2, length($2)-3)}' This script returns output like below to pull out "DEVICE NAME SERIAL". ...... hdisk28 110B hdisk29 1112 hdisk30 1115 hdisk31 1116 hdisk32 1128... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

4. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

5. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

6. UNIX for Dummies Questions & Answers

How to add/sum same contents in a unix file

Input 1 2N 2N 1 2N 2N 1 2N 2N 1 2N Ateus 1 3 Mobiles 3M-100 1 3 Mobiles 3M-100 1 3 Mobiles 3M-100 1 3GNET 3gnet 1 3GNET 3gnet 1 3GNET G2828 1 3GNET G2828 1 3GNET G2829 1 3GNET G2829 1 3GNET G2829 OutPut should be 3 2N 2N 3 3 Mobiles 3M-100 2 3GNET 3gnet (4 Replies)
Discussion started by: gagan2010
4 Replies

7. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

8. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

9. Programming

How to read and write directory or file contents in c++ ?

Dear Friends, I m beginner unix programmer. I want to know, how to read and write directory or file contents in c++ ? (3 Replies)
Discussion started by: namrata5
3 Replies

10. Shell Programming and Scripting

small script - get sql output & write into txt

Hi, how can I write a small script to run the following statement and output the result into check_result.txt select /*+RULE*/ tapname from typetbl where tapname like 'AA%' and rejectcode=9; Normally, I just type sql and get into SQL> (2 Replies)
Discussion started by: happyv
2 Replies
Login or Register to Ask a Question