[question] trouble with an 'exercise'

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [question] trouble with an 'exercise'
# 1  
Old 09-15-2007
Error [question] trouble with an 'exercise'

Hello guys..
well, im kinda newbie with unix because i started to learn it like 2 weeks ago.

then i started to make some exercises, but i got stucked on this one :

5. For each of those 3 files created on item 2:
a. Provide the TOTAL number of transactions for each sub-methods call. The sub-method call is listed in the fifth field of each line. Each field is separated by '|' (pipe) character.
b. For each sub-method call:
I. Provide the TOTAL number of SUCCESS.
II. Provide the TOTAL number of ERROR.
so, i need to know how many different 'names' has the 5th field and how many times each name appears.

i was trying with a "cut -d'|' -f5 filename | sort;" , but it only returns me all 5th field names ordered.

then i talked with few friends, they told me to use the awk command, i tried to read few manuals but i couldn't make it.

so, i would be grateful if anyone could help me with it.

and btw, sorry my bad english


well, i kept trying with the awk and got the different methods name on 5th field with :
awk -F'|' '{print $5}' file1 | sort | uniq;

now i "just" need to know how many times they appear =/

Last edited by EnioMarques; 09-15-2007 at 06:23 PM..
# 2  
Old 09-16-2007
awk -F'|' '{arr[$5]++} END {for(i in arr) printf("[%s] appeared %d times\n", i, arr[i])}' file1

# 3  
Old 09-16-2007
wow thanks a lot dude SmilieSmilie

but can you explain me how you did it??I wanna know how you used those indexes.

and can you give me a hand on exercise B too?? i need to know the total of ERRORS and SUCCESS.Success transactions are logged with the word INFO in the beginning of the line and and error transactions are with a ERROR instead.

i used a
grep -C INFO file1;
grep -c ERROR file1;

to know the total of errors and succes at the total,but now i need for each sub method =/

thanks in advance

Last edited by EnioMarques; 09-16-2007 at 03:59 PM..
# 4  
Old 09-17-2007
getting totals

For any command that returns a number of lines EG 'grep INFO' you can cound the number of lines with wc -l


grep INFO | wc -l

this will return the number of lines matching INFO
# 5  
Old 09-17-2007
ya i know, but i need to complement that A) exercise.

when i use vgersh99's code i get like :
[SubMethod1] appeared 100 times
[SubMethod3] appeared 123 times
[SubMethod2] appeared 15 times

for B) i need like..

[SubMethod1] appeared 100 times with 3 errors and 97 successful
[SubMethod2] appeared 123 times with 3 errors and 120 successful
[SubMethod3] appeared 15 times with 5 errors and 10 successful
# 6  
Old 09-17-2007
awk -F'|' '/^INFO/{arrS[$5]++} /^ERROR/{arrE[$5]++} {arr[$5]++}END {for(i in arr) printf("[%s] appeared %d times with %d errors and %d successful\n", i, arr[i], arrE[i], arrS[i] )}' file1

# 7  
Old 09-17-2007
thanks again vgersh Smilie

well, now i'm stucked on another one,this is kinda complicated for me =/
3.1- Total transactions per hour
3.2- Total transactions per business hour

the date/time is the 4th field with this format : yyyy-mm-dd hh:mm:ss,MMM (M=miliseconds).
also the 'business hour' is between 09:00:00,000 ~ 18:00:00.000.

thanks in advance


already did it by myself...here the code if any1 else has the same question:
cat filename | cut -d'|' -f4 | cut -d' ' -f2 |awk -F':' '{arr[$1]++} END {for(i in arr) printf("%s appears %d times.\n", i, arr[i])}' | sort ;

Last edited by EnioMarques; 09-19-2007 at 04:43 PM.. Reason: question added
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Grades exercise

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 shell script program that will input a name and a mark between 0 and 100. The program then displays the... (9 Replies)
Discussion started by: UniverseCloud
9 Replies

2. UNIX for Dummies Questions & Answers

UNIX Exercise

Hi, I am learning unix. I want to practice few small excercises. Please suggest me some goor URL to practice. Thanks in advance. (1 Reply)
Discussion started by: stew
1 Replies

3. Shell Programming and Scripting

A very tough exercise

hello everyone!:) I have an exercise which I think is difficult for beginner like me. Here is the exercise Create a shell script, which takes a directory as command line argument. Script displays ten first lines from every text file in that directory. After displaying the lines from the... (1 Reply)
Discussion started by: googlevn
1 Replies

4. Homework & Coursework Questions

Help with this exercise

you are to write a program which will read in a tax rate (as a percentage) and the prices of 5 items. the program is to calculate the total price, before tax, of the items and then the tax payable on those items, and then the total amount due. the tax payable is computed by appliying the tax rate... (1 Reply)
Discussion started by: bunkercrazy
1 Replies

5. Shell Programming and Scripting

Trouble with part of an exercise

Hi, 'm trying to do an exercicise, and one part is: ls -l $1 | awk ' BEGIN { max = $5; } { if ($5 > max){ max = $5; } } END { print "Tamanio mayor fichero = " max; }' # Imprimimos ahora el menor tamaño de fichero ls -l $1 | awk '... (4 Replies)
Discussion started by: Phass
4 Replies

6. UNIX for Dummies Questions & Answers

[question] hard exercise, help needed

Hello guys. Well, on this exercise i need the average "chargeAmount" per hour (for each hour). with this code : cat getusagesummarywrongmatch | grep -iv MOU2GRTObject | cut -d'|' -f4,14 | grep -i chargeamount | cut -d' ' -f2 http://img227.imageshack.us/img227/5889/65969235do0.jpg i got... (2 Replies)
Discussion started by: EnioMarques
2 Replies

7. UNIX for Dummies Questions & Answers

The trouble about SU ...

Hi all, having read lots of posts about SU I don't quiet understand this : I'm doing regular backups of my database (u betta do) and therefore use su - username -c "sqlscript special data_base" in a unixscript which is even using cron. (yep!) Now I need some other script, still with this... (4 Replies)
Discussion started by: nulnul7
4 Replies

8. What is on Your Mind?

The trouble with...

Welcome to "The trouble with...." with your host, ZazzyBob. Todays offering - "The trouble with letting other people host your website" I use a certain web hosting service, who shall of course remain nameless here. They are running PHP 4.3.10 I decide to write a script to test their PHP... (6 Replies)
Discussion started by: zazzybob
6 Replies
Login or Register to Ask a Question