LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk - how to join the string with calculated values (https://www.linuxquestions.org/questions/programming-9/awk-how-to-join-the-string-with-calculated-values-4175461885/)

kcapple 05-14-2013 03:34 AM

awk - how to join the string with calculated values
 
Hi guys, I'm new to unix programming. There a question I would like to ask. I have an input file and the format is

fruits quantities.prices

Code:

Apple 5.2
Orange 4.4
Watermelon 3.10
Berries 10.2

I'm require to multiple the quantities by 2 and calculate the total price and output the file in the format:
Code:

Apple 5.2(10) Orange 4.4(8)
Watermelon 3.10(30) Berries 10.2(20)

And this is my ouput:
Code:

Apple 5.2 Orange 4.4
Watermelon 3.10 Berries 10.2

This is my code so far:
Code:

amount = substr($NF, 1, 2)
  total_amount = amount * 2

  if( total_amount <= 18 )
  {
        price = substr($NF,3)
  }
  else
  {
        price = substr($NF, 4)
  }

  total = total_amount * price
  getline a, total

Any help with the total price?

pan64 05-14-2013 03:51 AM

I would recommend you use another field separator ( FS="[ .]" ). In that case you will have $1 fruit, $2 quantity, $3 price. And you can easily calculate total...

kcapple 05-14-2013 04:27 AM

Thanks for the tips, it make my code look much simpler

Quote:

Originally Posted by pan64 (Post 4950693)
I would recommend you use another field separator ( FS="[ .]" ). In that case you will have $1 fruit, $2 quantity, $3 price. And you can easily calculate total...

But I still can't get the desire output:
Code:

Apple 5.2(10) Orange 4.4(8)
Watermelon 3.10(30) Berries 10.2(20)


pan64 05-14-2013 04:36 AM

What did you try?

kcapple 05-14-2013 06:07 AM

Quote:

Originally Posted by pan64 (Post 4950725)
What did you try?

The code is much shorter compare with before:

Code:

 
  FS = "[ .]"
  total_amount = $2 * 2

  total = total_amount * $3
  getline a
  print a, $1, $2, $3 , $4, total


pan64 05-14-2013 06:10 AM

so you need to modify printing, first you save the result in a variable, next you print the variable and the current result and start over

kcapple 05-14-2013 07:10 AM

Quote:

Originally Posted by pan64 (Post 4950776)
so you need to modify printing, first you save the result in a variable, next you print the variable and the current result and start over

I've tried to store the result in variable and reprint it with the current result. It only show the total for the Apple and Watermelon but not the Orange and berries.
Code:

FS = "[ .]"
  total_amount = $2 * 2
  total = total_amount * $3
  result = $($1 $2 $3 $4 total)
 
  getline
  print result, $1, $2, $3, $4, total


pan64 05-14-2013 07:15 AM

how did you execute it?

kcapple 05-14-2013 07:19 AM

awk -f fruit fruits

I save my script as fruit, and the input file as fruits

pan64 05-14-2013 07:24 AM

that does not work for me. which version did you try? (awk --version)

kcapple 05-14-2013 07:26 AM

I'm running in version 3.1.6

pan64 05-14-2013 07:40 AM

so first, FS should be set only once, you need to write:
BEGIN { FS = "...... }
next, the code should be also enclosed into { }
finally awk automatically implies getline, so you should not use it at all

kcapple 05-14-2013 08:16 AM

Quote:

Originally Posted by pan64 (Post 4950846)
so first, FS should be set only once, you need to write:
BEGIN { FS = "...... }
next, the code should be also enclosed into { }
finally awk automatically implies getline, so you should not use it at all

Did you mean it like this?
Code:

BEGIN{ FS ="[ .]" }
{
 
  total_amount = $2 * 2
  total = total_amount * $3
  result = $($1 $2 $3)

  print result, total, $1, $2, $3, total
}

If without the getline, the output will be duplicated.

Code:

Apple 5.2 20 Apple 5 2 20
Orange 4.4 32 Orange 4 4 32
Watermelon 3.10 60 Watermelon 3 10 60
Berries 10.2 40 Berries 10 2 40

*I'm sorry for taking so much of your time :(*

pan64 05-14-2013 08:48 AM

you do not need to say sorry but rethink your print statement. What do you really want to print?
ok, I missed, probably you can try getline, just you need to count odd and even lines - and also handle the line read by getline, that was missing before

kcapple 05-15-2013 03:21 AM

Quote:

Originally Posted by pan64 (Post 4950899)
you do not need to say sorry but rethink your print statement. What do you really want to print?
ok, I missed, probably you can try getline, just you need to count odd and even lines - and also handle the line read by getline, that was missing before

Thanks a lot pan64! I've managed to solve it with all your tips!:D


All times are GMT -5. The time now is 11:39 PM.