Sponsored Content
Full Discussion: Division problem -Awk
Top Forums Shell Programming and Scripting Division problem -Awk Post 302496949 by alister on Tuesday 15th of February 2011 08:30:33 PM
Old 02-15-2011
While both Franklin52's and rdcwayx's solutions work for me, neither should, according to POSIX.

If i'm not mistaken, the portable way to write
Code:
!$1 || !$2 || !$3 || !$4

is
Code:
!($1+0) || !($2+0) || !($3+0) || !($4+0)

to explicitly demand that each field variable be treated as a number.

The standard says that in a boolean context (such as the logical not), if the type is a number, 0 is false and all others are true; if the type is a string, the null string is false and all others are true. There is no conversion of type (as there is when comparing, in which case a 'numeric string' is coerced to a number if the other value is a number). So when a field is "0", that's a true value in a boolean context since it's a non-empty string.

I think I'd just go with an explicit comparison to zero, $i == 0, since it's both clear and portable (the hardcoded number 0 ensures that the field value is treated as a number).

For more detailed info, refer to the 'Expressions in awk' section @ http://pubs.opengroup.org/onlinepubs...ities/awk.html

Wow, that's a lot of typing for a little division. Smilie

Regards,
Alister

Last edited by alister; 02-15-2011 at 10:32 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

division problem

how can i show the value when i divide a number where the dividend is greater then the divisor. for example... 3 divided by 15 ---> let x=3/15 when i do this in the shell environment it gives me an output of 0. please help me. thanks. (3 Replies)
Discussion started by: inquirer
3 Replies

2. UNIX for Dummies Questions & Answers

Problem in division

hi I am having two variables namely a=7 & b=8. I have to subtract these two variables. I am using the command c=`expr $a / $b` When I check the value of c, it comes out to be zero. Please help. Regards Rochit (9 Replies)
Discussion started by: rochitsharma
9 Replies

3. Shell Programming and Scripting

awk Division and modulus

I need to read the file divide 3 column with 2nd and run a modulus of 10 and check whether the remainder is zero or not if not print the entire line. cat filename | awk '{ if ($3 / $2 % 10 != 0) print $0}' Whats wrong with it ? (4 Replies)
Discussion started by: dinjo_jo
4 Replies

4. Shell Programming and Scripting

hash map Illegal division by zero problem

Hi Everyone, a.txt line1;a;33 line1;c;22 line1;b;0 line1;a;55 a.pl #!/usr/bin/perl use strict; use warnings; my @sorted=(); my @tmp; my $FA; my @F; (0 Replies)
Discussion started by: jimmy_y
0 Replies

5. UNIX for Advanced & Expert Users

awk: division by zero

I received error "awk: division by zero" while executing the following statement. SunOS 5.10 Generic_142900-15 sun4us sparc FJSV,GPUZC-M echo 8 | awk 'END {printf ("%d\n",NR/$1 + 0.5);}' file1.lst awk: division by zero Can someone provide solution? Thanks Please use code... (11 Replies)
Discussion started by: kumar77
11 Replies

6. Shell Programming and Scripting

awk division error - 0

input 0 0 9820373 2069 0 0 11485482 awk '{print ($1/$3) / ($4/$7)}' input error Is there any way to fix this problem ? (25 Replies)
Discussion started by: quincyjones
25 Replies

7. Shell Programming and Scripting

awk & division

vmstat|awk '{print $3}'|tail -1 returns 6250511, but what I need is 24416, which is 6250511 divided by 256. Please advise. Thank you so much (2 Replies)
Discussion started by: Daniel Gate
2 Replies

8. Shell Programming and Scripting

variables division with awk

hello i try to divide 2 variables in order to get a percentage--that's why i'm not interested in integer division--but nothing seems to work I think awk is suitable for this but i'm not quite sure how to use it.. any ideas? here's what I want to do: percentage = varA/varB thank you (2 Replies)
Discussion started by: vlm
2 Replies

9. Shell Programming and Scripting

awk - Division with condition

Hi Friends, I have an input file like this cat input chr1 100 200 1 2 chr1 120 130 na 1 chr1 140 160 1 na chr1 170 180 na na chr1 190 220 0 0 chr1 220 230 nd 1 chr2 330 400 1 nd chr2 410 450 nd nd chr3 500 700 1 1 I want to calculate the division of 4th and 5th columns. But, if... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

10. Shell Programming and Scripting

awk Division By Zero Bypass

Hi Friends, I don't understand why "a" is always being printed as zero, when I execute the following command. awk '{if($6||$8||$10||$12==0)a=b=c=d=0;else (a=$5/$6);(b=$7/$8);(c=$9/$10);(d=$11/$12); {print... (6 Replies)
Discussion started by: jacobs.smith
6 Replies
AUSEARCH-EXPRESSION(5)						    Linux Audit 					    AUSEARCH-EXPRESSION(5)

NAME
ausearch-expression - audit search expression format OVERVIEW
This man page describes the format of "ausearch expressions". Parsing and evaluation of these expressions is provided by libauparse and is common to applications that use this library. LEXICAL STRUCTURE
White space (ASCII space, tab and new-line characters) between tokens is ignored. The following tokens are recognized: Punctuation ( ) Logical operators ! && || Comparison operators < <= == > >= !== i= i!= r= r!= Unquoted strings Any non-empty sequence of ASCII letters, digits, and the _ symbol. Quoted strings A sequence of characters surrounded by the " quotes. The character starts an escape sequence. The only defined escape sequences are \ and ". The semantics of other escape sequences is undefined. Regexps A sequence of characters surrounded by the / characters. The character starts an escape sequence. The only defined escape sequences are \ and /. The semantics of other escape sequences is undefined. Anywhere an unquoted string is valid, a quoted string is valid as well, and vice versa. In particular, field names may be specified using quoted strings, and field values may be specified using unquoted strings. EXPRESSION SYNTAX
The primary expression has one of the following forms: field comparison-operator value egexp string-or-regexp field is either a string, which specifies the first field with that name within the current audit record, or the escape character fol- lowed by a string, which specifies a virtual field with the specified name (virtual fields are defined in a later section). field is a string. operator specifies the comparison to perform r= r!= Get the "raw" string of field, and compare it to value. For fields in audit records, the "raw" string is the exact string stored in the audit record (with all escaping and unprintable character encoding left alone); applications can read the "raw" string using auparse_get_field_str(3). Each virtual field may define a "raw" string. If field is not present or does not define a "raw" string, the result of the comparison is false (regardless of the operator). i= i!= Get the "interpreted" string of field, and compare it to value. For fields in audit records, the "interpreted" string is an "user- readable" interpretation of the field value; applications can read the "interpreted" string using auparse_interpret_field(3). Each virtual field may define an "interpreted" string. If field is not present or does not define an "interpreted" string, the result of the comparison is false (regardless of the operator). < <= == > >= !== Evaluate the "value" of field, and compare it to value. A "value" may be defined for any field or virtual field, but no "value" is currently defined for any audit record field. The rules of parsing value for comparing it with the "value" of field are specific for each field. If field is not present, the result of the comparison is false (regardless of the operator). If field does not define a "value", an error is reported when parsing the expression. In the special case of egexp regexp-or-string, the current audit record is taken as a string (without interpreting field values), and matched against regexp-or-string. regexp-or-string is an extended regular expression, using a string or regexp token (in other words, delimited by " or /). If E1 and E2 are valid expressions, then ! E1, E1 && E2, and E1 || E2 are valid expressions as well, with the usual C semantics and evalu- ation priorities. Note that ! field op value is interpreted as !(field op value), not as (!field) op value. VIRTUAL FIELDS
The following virtual fields are defined: imestamp The value is the timestamp of the current event. value must have the ts:seconds.milli format, where seconds and milli are decimal numbers specifying the seconds and milliseconds part of the timestamp, respectively. ecord_type The value is the type of the current record. value is either the record type name, or a decimal number specifying the type. SEMANTICS
The expression as a whole applies to a single record. The expression is true for a specified event if it is true for any record associated with the event. EXAMPLES
As a demonstration of the semantics of handling missing fields, the following expression is true if field is present: (field r= "") || (field r!= "") and the same expression surrounded by !( and ) is true if field is not present. FUTURE DIRECTIONS
New escape sequences for quoted strings may be defined. For currently defined virtual fields that do not define a "raw" or "interpreted" string, the definition may be added. Therefore, don't rely on the fact that comparing the "raw" or "interpreted" string of the field with any value is false. New formats of value constants for the imestamp virtual field may be added. AUTHOR
Miloslav Trmac Red Hat Feb 2008 AUSEARCH-EXPRESSION(5)
All times are GMT -4. The time now is 02:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy