-2

I have a file that looks like

01/11/2015;998978000000;4890********3290;5735;ITUNES.COM/BILL;LU;Cross_border_rub;4065;17;915;INSUFF FUNDS;51;0;

There are 13 semicolon separated columns.

I'm trying to calculate 9 columns for all lines:

awk -F ';' -vOFS=';' '{ gsub(",", ".", $9); print }' file | 
awk -F ';' '$0 = NR-1";"$0' | 
awk -F ';' -vOFS=';' '{bar[$1]=$1;a[$1]=$2;b[$1]=$3;c[$1]=$4;d[$1]=$5;e[$1]=$6;f[$1]=$7;g[$1]=$8;h[$1]=$9;k[$1]=$10;l[$1]=$11;l[$1]=$12;m[$1]=$13;p[$1]=$14;};
if($7="International") {income=0.0162*h[i]+0.0425*h[i]};
else if($7="Domestic") {income=0.0188*h[i]};
else if($7="Cross_border_rub") {income=0.0162*h[i]+0.025*h[i]}
END{for(i in bar) print income";"a[i],b[i],c[i],d[i],e[i],f[i],g[i],h[i],k[i],l[i],m[i],p[i]}'

How exactly do multiple if statements correctly work in awk?

peak
  • 105,803
  • 17
  • 152
  • 177
qiker
  • 15
  • 5
  • 1
    what is `i` in the main loop (before the `END` block)? – karakfa Mar 10 '16 at 21:01
  • 1
    i think you'll get better help if you specify what do you want to compute. I can see many inefficiencies in the posted code. – karakfa Mar 10 '16 at 21:03
  • Thanks for your attention!For all unique line in file I want print calculate "income" in new 1-st column, which depends at value of 7 column ($7) – qiker Mar 10 '16 at 21:04
  • 1
    `if () ... else if () ... ` works fine in awk, but the conditionals you show can be better placed as patterns. '$7 == "International" { income=0.0162*h[i]+0.0425*h[i]}' – mpez0 Mar 10 '16 at 21:07
  • @mpez0 Thanks?but how can I use this in "FOR" loop? – qiker Mar 10 '16 at 21:08
  • 1
    $7 will only have one value per line -- you can think of awk as a for loop over each line in the input file. – mpez0 Mar 10 '16 at 21:10
  • 1
    In the future, learn to use the `{}` tool at the top left of the edit box on highlighted text to keep proper formatting for code/data/errMsgs/etc. Also, just because your data has 19 columns, is the problem so unique that all values are important to generate a solution? If you really have a 19-way problem, maybe you need to look into hiring a consultant. Normally, 3-4 columns should be enough AS WELL AS expected output from that data. OK?!? :-) Please and thank you. Good luck. – shellter Mar 10 '16 at 21:16
  • Just the other day I posted this basic overview of [tag:awk]: https://stackoverflow.com/questions/35819687/awk-equivalent-of-ltrim-function-in-c/35828153#35828153; it explains the basic structure of an awk program, including how multiple *pattern* `{` *action* `}` pairs work. – Henk Langeveld Mar 10 '16 at 21:29

1 Answers1

4

awk to the rescue!

You don't need the multiple awk invocations. Can consolidate into one

$ awk -F';' -v OFS=';' '{gsub(",", ".", $9)}
      $7=="International" {income=(0.0162+0.0425)*$9}
      $7=="Domestic" {income=0.0188*$9}
      $7=="Cross_border_rub" {income=(0.0162+0.025)*$9}
  #  what happens for other values since previous income will be copied over
      {print income, NR-1, $0}' file

test with your file since you didn't provide a enough sample to test.

Perhaps better if you just assign the rate

$ awk -F';' -v OFS=';' '{gsub(",", ".", $9); rate=0}
      $7=="International" {rate=0.0162+0.0425}
      $7=="Domestic" {rate=0.0188}
      $7=="Cross_border_rub" {rate=0.0162+0.025}
      {print rate*$9, NR-1, $0}' file
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Thanks!!! But can you ask me, how can I do it using for(i in a) and multiple statements/ – qiker Mar 10 '16 at 21:22
  • and what we can do, if in column more than 3 value? I think must to use if - else – qiker Mar 10 '16 at 21:25
  • that will be the default case, initialize the default case in the first block (instead of zero) and the other matches will override the default. Since they are mutually exclusive only one of them will be active. You don't need for loop in your case (nor END block) if you're working one line at a time. – karakfa Mar 10 '16 at 21:33
  • Were you able to test the second script? – karakfa Mar 10 '16 at 21:34