Sponsored Content
Top Forums Shell Programming and Scripting Trying to learn to use functions in gawk and not getting expected output. Post 302842833 by DeCoTwc on Saturday 10th of August 2013 02:40:13 PM
Old 08-10-2013
Trying to learn to use functions in gawk and not getting expected output.

I've been working on improving my awk, and the next thing I want to learn is to properly use functions (I understand functions in shell and python). I have the following code which includes how I did this without functions before, and two attempts I've made to do it with functions:

Code:
function div(n, d)
{
  if (n == 0) {
  	return n
  } else {
  	return substr(n / d * 100, 0, 5)
  }
}

BEGIN{ 
    OFS=",";SUBSEP=","
     }
      $7   == 17018      {SETUP[$1,substr($2,0,2)]++ ; DT[$1,substr($2,0,2)]++ }
      $7   == 17030      { SUCC[$1,substr($2,0,2)]++ ; DT[$1,substr($2,0,2)]++ }
      $7   == 17120      {  FWD[$1,substr($2,0,2)]++ ; DT[$1,substr($2,0,2)]++ ; SETUP[$1,substr($2,0,2)]-- }
      $7   == 17014      { TIME[$1,substr($2,0,2)]++ ; DT[$1,substr($2,0,2)]++ ;  SUCC[$1,substr($2,0,2)]-- }
      $19  == "(Missing" { MISS[$1,substr($2,0,2)]++ ; DT[$1,substr($2,0,2)]++ }
END{
    for (X in DT)
    #print X,SETUP[X]+0,SUCC[X]+0,substr(SUCC[X]/SETUP[X]*100,0,5),FWD[X]+0,TIME[X]+0,MISS[X]+0 #Old way ->  Works
    perc = div(SUCC[X],SETUP[X]) #New Way  
    print X,SETUP[X]+0,SUCC[X]+0,perc,FWD[X]+0,TIME[X]+0,MISS[X]+0 #New Way  -> doesn't work

   }

Below is the output I get with both:

Code:
[root@decobox utils]# cat /opt/decobox/logs/manager.log|awk -f test.streamReport.awk|sort -M #Old way works, and gives full report
08/10/13,01,542,536,98.89,361,0,0
08/10/13,02,330,327,99.09,210,0,0
08/10/13,03,235,232,98.72,104,0,0
08/10/13,04,181,181,100,94,0,0
08/10/13,05,180,179,99.44,104,0,1
08/10/13,06,322,320,99.37,181,1,1
08/10/13,07,475,468,98.52,344,0,1
08/10/13,08,671,663,98.80,436,0,2
08/10/13,09,745,734,98.52,401,3,5
08/10/13,10,773,761,98.44,398,0,2
08/10/13,11,717,713,99.44,397,0,4
08/10/13,12,783,764,97.57,447,7,5
08/10/13,13,719,694,96.52,450,1,7
08/10/13,14,393,379,96.43,191,8,5
[root@decobox utils]# vim test.streamReport.awk
[root@decobox utils]# cat /opt/decobox/logs/manager.log|awk -f test.streamReport.awk|sort -M #New way only gives one line of output
08/10/13,06,322,320,99.37,181,1,1

I can't seem to figure out why this isn't working. I'm sure it's something simple, but I'm at a loss here.

Somewhat as an aside, how does my code look here? Is there anything obviously wrong, or things I could be doing better? I'm not sure that returning a value from a function instead of printing it is preferred in awk, but when I tried getting it to work by printing things out, it broke completely.
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

GAWK removes FS | on output

I have the simple gawk script below. When the script runs in the output of all the ITM lines the FS is replaced with a space, the Non ITM lines retain the | field separator. The ITM lines have many fields and I can't insert "|" between each field because some of the fields are blank. Is... (1 Reply)
Discussion started by: paulr211
1 Replies

2. Shell Programming and Scripting

awk not generating the expected output

Hi, I am presently stuck in a csv file. INPUT CSV baseball,NULL,8798765,Most played baseball,NULL,8928192,Most played baseball,NULL,5678945,Most played cricket,NOTNULL,125782,Usually played cricket,NOTNULL,678921,Usually played EXPECTED OUTPUT CSV ... (7 Replies)
Discussion started by: scripter12
7 Replies

3. Shell Programming and Scripting

Output is not comming as expected

Hi All, I am in middle of one script. I want output in the form of xls file. There are 4 fields - user name, email Id, full name, date of birth. I want these details to get in seperate columns. But, i am getting it in the single cell and as like a paragraph.:mad: Please suggest me some... (8 Replies)
Discussion started by: Agupte
8 Replies

4. Shell Programming and Scripting

Not getting expected output

Hi I have written below script to get the data in table form. #!/bin/sh echo "File Name\tType" for i in *; do echo "$i\t\c" if ; then echo "directory" elif ; then echo "symbolic link" elif ; then echo "file" else echo "unknown" fi donehowever i am getting output in different way... (3 Replies)
Discussion started by: scriptor
3 Replies

5. Shell Programming and Scripting

Gawk output difference

Why the below option2 doesn't fetch similar output as option1 ? I am on linux. $cat test 2013-01-01-00.25.43.643845 Option1: cat test | gawk -F"-" ' {print $2 " " $3 " " $1}' 01 01 2013 Option2: cat test | gawk '{FS="-"} {print $2 " " $3 " " $1}' 2013-01-01-00.25.43.643845 (5 Replies)
Discussion started by: Shivdatta
5 Replies

6. Shell Programming and Scripting

Local variable in functions (gawk)

Hi Everybody :) I need your help, because i know a local variable in a function for example k, it is different of other variable(with the same name k) this a global variable. Is that right? dgawk> run Starting program: 3238860128818202 3 4 7 11 12 13 17 22 23 32 35 37 41 48 49 55 63 ... (5 Replies)
Discussion started by: solaris21
5 Replies

7. Shell Programming and Scripting

awk output not what was expected

Good Moring, I am currently reading about awk in a manual and following the examples using the oratab file. My system is SOLARIS 10 I think I am getting strange behavior judging by what the book says to do and what I am getting with my little program. Here is my program: grep -v oratab |... (4 Replies)
Discussion started by: bdby
4 Replies

8. Shell Programming and Scripting

Gawk output separated by tab

In the gawk below, I am trying to output the file tab-deliminated but don't think that is the correct syntax. Thank you :). gawk OFS='/t' '{sub(/-+/,"",$2); ar=$0} END{n = asort(ar) for (i = 1; i <= n; i++) print ar}' file (2 Replies)
Discussion started by: cmccabe
2 Replies

9. Shell Programming and Scripting

For loop not giving expected output

#cat /tmp/input old_array old_dev new_dev new_array 0577 008AB 01744 0125 0577 008AC 01745 0125 0577 008AD 005C8 0125 0577 008AE 005C9 0125 0577 008AF 005CA 0125 0577 008B0 005CB 0125 0577 008B1 005CC 0125 cat test.sh #!/bin/ksh... (4 Replies)
Discussion started by: mbak
4 Replies

10. Shell Programming and Scripting

awk not giving the output expected

Hello, I am practising awk and decided to compare two columns and print the result of the comparison as third column i/p data c1,c2,c3 1,a,b 1,b,b i am trying to compare the last two columns and if they match I am trying to print match else mismatch(Ideally i want that as a last column... (5 Replies)
Discussion started by: mkathi
5 Replies
mailq(1)							   User Commands							  mailq(1)

NAME
mailq - print the mail queue SYNOPSIS
/usr/bin/mailq [-Ac] [-q subarg] [-v] DESCRIPTION
The mailq utility displays a summary of the mail messages queued for future delivery. The first line displayed for each mail message shows the internal identifier used on this host for the message, the size of the message in bytes, the date and time the message was accepted into the queue, and the envelope sender of the message. The second line of the display shows the error message that caused this message to be retained in the queue. This line will not be displayed if the message is being pro- cessed for the first time. The mailq utility used to be identical to sendmail -bp. Now it checks for the authorization attribute, solaris.mail.mailq. If the check for the invoking user succeeds, sendmail -bp is executed with the remaining argument vector. Otherwise, an error message is printed. This authorization attribute is by default enabled for all users. It can be disabled by modifying the Basic Solaris User entry in prof_attr(4). OPTIONS
The following options are supported: -Ac Like sendmail(1M), this flag tells mailq to use submit.cf rather than sendmail.cf even if the operation mode does not indicate an initial mail submission. This will result in the client queue /var/spool/clientmqueue being displayed rather than the default server queue /var/spool/mqueue. -qp[time] Similar to -qtime, except that instead of periodically forking a child to process the queue, sendmail forks a single per- sistent child for each queue that alternates between processing the queue and sleeping. The sleep time is given as the argument. The sleep time default is 1 second. The process will always sleep at least 5 seconds if the queue was empty in the previous queue run. -qf Processes saved messages in the queue once and does not fork(), but runs in the foreground. -qG name Processes jobs in the queue group called name only. -q[!]I substr Limits processed jobs to those containing substr as a substring of the queue id, or not when ! is specified. -q[!]R substr Limits processed jobs to those containing substr as a substring of one of the recipients, or not when ! is specified. -q[!]S substr Limits processed jobs to those containing substr as a substring of the sender, or not when ! is specified. -v Prints verbose information. This adds the priority of the message and a single character indicator (+ or blank) indicating whether a warning message has been sent on the first line of the message. Additionally, extra lines may be intermixed with the recipients that indicate the "controlling user" information. This shows who will own any programs that are executed on behalf of this message and the name of the alias this command is expanded from, if any. EXIT STATUS
0 Successful completion. >0 An error occurred. FILES
/etc/security/prof_attr local source for execution profile attributes /var/spool/mqueue default server queue /var/spool/clientmqueue client queue ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWsndmu | +-----------------------------+-----------------------------+ SEE ALSO
sendmail(1M), prof_attr(4), attributes(5) SunOS 5.11 10 Jul 2002 mailq(1)
All times are GMT -4. The time now is 04:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy