Conditional Arithmetic in [g]awk

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Conditional Arithmetic in [g]awk
# 1  
Old 02-15-2018
Conditional Arithmetic in [g]awk

I am having a difficult time getting an awk one-liner to work correctly that runs a mathematical operation upon values in a field when matching a given criteria.

I would like to subtract 1 from every value in field $6 that is greater than 12. In this particular case it is only a constant of value 1, and I suppose this can be accomplished with a decrement, but I would like to learn how to do this with any numerical constant. If possible, I would also very much appreciate being pointed in the right direction should I want a similar conditional operation to be run concurrently on multiple fields. That is to say, if I would like to subtract 1 from every value in $6 greater than 12 and (&&) say, add 2 to every value in $1 that is less than 11. In other words, would it be possible to indicate how I may have the skeletal syntax for additional conditional operations? Thus, my data looks like this:

Code:
    7 100   1   1   7   6 509  1 509  1 501  2 501  2 505  3 505  3  -1 -1  -1 -1
    8 100   1   1   8   7 509  1 509  1 501  2 501  2 504  3 504  3  -1 -1  -1 -1
    9 100   2   2   9   8 501  1 501  1 503  2 503  2  -1 -1  -1 -1
   10 100   2   2  10   9 501  2 509  0 502  0 501  1 504  3 504  3  -1 -1  -1 -1
   11 100   2   2  11  10 501  3 509  0 502  0 509  0 503  4 501  1 504  5 503  3  -1 -1 505  4  -1 -1  -1 -1
   12 100   3   3  12  11 510  1 510  1 501  2 501  2 503  3 503  3 505  4 505  4  -1 -1  -1 -1
   13 100   3   3  13  12 521  2 519  0 503  3 521  1 504  4 503  2  -1 -1 505  3  -1 -1  -1 -1
   14 100   3   3  14  13 509  1 509  1 502  2 502  2 521  3 521  3  -1 -1  -1 -1
   15 100   4   4  15  14 501  1 501  1 504  3 503  0 505  0 504  2  -1 -1  -1 -1
   16 100   4   4  16  -1 505  0  -1 -1 501  0  -1 -1 504  0  -1 -1  -1 -1  -1 -1
   17 100   4   4  17  -1 501  0  -1 -1 503  0  -1 -1 504  0  -1 -1  -1 -1  -1 -1
   18 100   4   4  -1  15  -1 -1 509  0  -1 -1 504  0  -1 -1  -1 -1

My desired output is (NB: the change in font color is only to emphasize the desired operation relative to input):
Code:
    7 100   1   1   7   6 509  1 509  1 501  2 501  2 505  3 505  3  -1 -1  -1 -1
    8 100   1   1   8   7 509  1 509  1 501  2 501  2 504  3 504  3  -1 -1  -1 -1
    9 100   2   2   9   8 501  1 501  1 503  2 503  2  -1 -1  -1 -1
   10 100   2   2  10   9 501  2 509  0 502  0 501  1 504  3 504  3  -1 -1  -1 -1
   11 100   2   2  11  10 501  3 509  0 502  0 509  0 503  4 501  1 504  5 503  3  -1 -1 505  4  -1 -1  -1 -1
   12 100   3   3  12  11 510  1 510  1 501  2 501  2 503  3 503  3 505  4 505  4  -1 -1  -1 -1
   13 100   3   3  13  12 521  2 519  0 503  3 521  1 504  4 503  2  -1 -1 505  3  -1 -1  -1 -1
   14 100   3   3  14  12 509  1 509  1 502  2 502  2 521  3 521  3  -1 -1  -1 -1
   15 100   4   4  15  13 501  1 501  1 504  3 503  0 505  0 504  2  -1 -1  -1 -1
   16 100   4   4  16  -1 505  0  -1 -1 501  0  -1 -1 504  0  -1 -1  -1 -1  -1 -1
   17 100   4   4  17  -1 501  0  -1 -1 503  0  -1 -1 504  0  -1 -1  -1 -1  -1 -1
   18 100   4   4  -1  14  -1 -1 509  0  -1 -1 504  0  -1 -1  -1 -1

I have attempted:

Code:
awk '{if($6>12); $6-=1; print}' File

Code:
awk '{if($6>12){$6=-1; print}}' File

Code:
awk '{x=$6; if(x>12); x=-1}{print}' File

# 2  
Old 02-16-2018
Code:
$ awk '{if ($6 > 12) { $6--}; if ($1 < 11) {$1+=2} print } ' File

This User Gave Thanks to abdulbadii For This Post:
# 3  
Old 02-16-2018
Try also
Code:
awk '{$6 -= ($6>12); $1 += ($1<11)?2:0; print } ' OFS="\t" file

EDIT: or even (given that $1 won't assume -2)

Code:
awk '($6 -= ($6>12)) && ($1 += ($1<11)?2:0)' OFS="\t" file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-16-2018
Quote:
Originally Posted by RudiC
Try also
Code:
awk '{$6 -= ($6>12); $1 += ($1<11)?2:0; print } ' OFS="\t" file

EDIT: or even (given that $1 won't assume -2)

Code:
awk '($6 -= ($6>12)) && ($1 += ($1<11)?2:0)' OFS="\t" file

These can also be written as:
Code:
awk '{$6 -= ($6>12); $1 += 2*($1<11); print } ' OFS="\t" file
awk '($6 -= ($6>12)) && $1 += 2*($1<11)' OFS="\t" file

under the same conditions.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to do arithmetic operation

Hi, I've this following text file FileVersion = 1.03 Filetype = meteo_on_curvilinear_grid TIME = 0 hours since 2016-10-03 12:00:00 +00:00 -6.855 -6.828 -6.801 -6.774 -6.747 -6.719 -6.691 -6.663 -6.634 -6.606 -6.577 -6.548 -6.519 -6.489 TIME = 0 hours since... (2 Replies)
Discussion started by: xisan
2 Replies

2. UNIX for Dummies Questions & Answers

Basic arithmetic operation with awk?

input: Name|Operation rec_10|1+2+2- Output: rec_10|1 Basically I am trying to calculate the result of "the path" in $3 where the operators follow the number and not preceding them like we normally do: rec_10: +1+2-2=1 But I realise (I am sure there is a good reason for that) that awk... (7 Replies)
Discussion started by: beca123456
7 Replies

3. Shell Programming and Scripting

Conditional awk

Hello All, I have a file like this: bash-3.00$ cat 1.txt 201112091147|0|1359331220|1025 201112091147|0|1359331088|1024 201112091144|0|1359331172|1025 201112091147|0|1359331220|1021 201112091149|0|1359331088|1027 201112091144|0|1359331172|1029 and a list of MSISDNs in another file... (9 Replies)
Discussion started by: EAGL€
9 Replies

4. Shell Programming and Scripting

PAssing variables to awk arithmetic

Hi all, I am wanting to pass variables from a file to an awk arithmetic formula. When I use the formula with the value it works well. As soon as I make these variables I get an inf (infinity) response. I can certainly echo the variables back and they look correct. My googling for answers has... (3 Replies)
Discussion started by: gafoleyo73
3 Replies

5. Programming

arithmetic calculation using awk

hi there again, i need to do a simple division with my data with a number of rows. i think i wanted to have a simple output like this one: col1 col2 col3 val1 val2 val1/val2 valn valm valn/valm any suggestion is very much appreciated. thanks much. (2 Replies)
Discussion started by: ida1215
2 Replies

6. Shell Programming and Scripting

arithmetic from csh variable passed to awk

I have the following code in a csh script I want to pass the value of the variable sigmasq to the awk script so that I can divide $0 by the value of sigmasq grep "Rms Value" $f.log \ | awk '{ sub(/*:*\.*/,x); \ print... (2 Replies)
Discussion started by: kristinu
2 Replies

7. Shell Programming and Scripting

Arithmetic operation with awk

I have output like following in a file usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:23:58 usmtnz-dinfsi19 72 71 38 1199 1199 0.8 19:24:04 (9 Replies)
Discussion started by: fugitive
9 Replies

8. Shell Programming and Scripting

sed or awk for arithmetic task

I have file with this type of format 01.02.09 08:30 bob jill mark 01.04.09 07:00 bob jill mark tom I want to count the names after the date /ime line (01.02.09 08:30) and add that number after the time like this 01.02.09 08:30 3 01.04.09 07:00 4 I don't care about... (6 Replies)
Discussion started by: marcelino
6 Replies

9. Shell Programming and Scripting

Awk Conditional

Hi Guys, i have this files: xyz20080716.log opqrs20080716.log abcdef20080716.log xyz20080717.log oprs20080717.log abcde20080717.log currentdate: 20080717.log I want to make script to zip the file for past day. Can anyone help for this? i've just learn awk scripting & still confused with... (3 Replies)
Discussion started by: icy_blu_blu
3 Replies

10. Shell Programming and Scripting

AWK - conditional cause

Hello guys, I want to make a conditional cause in the following file using awk: awk '{ if ($2 != 0) print $1, $2, $3}' test.csv > test2.csv FILE EXAMPLE = test.csv string,number,date abc,0,20050101 def,1,20060101 ghi,2,20040101 jkl,12,20090101 mno,123,20020101 ... (2 Replies)
Discussion started by: Rafael.Buria
2 Replies
Login or Register to Ask a Question