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 Code:
Apple 5.2(10) Orange 4.4(8) Code:
Apple 5.2 Orange 4.4 Code:
amount = substr($NF, 1, 2) |
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...
|
Thanks for the tips, it make my code look much simpler
Quote:
Code:
Apple 5.2(10) Orange 4.4(8) |
What did you try?
|
Quote:
Code:
|
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
|
Quote:
Code:
FS = "[ .]" |
how did you execute it?
|
awk -f fruit fruits
I save my script as fruit, and the input file as fruits |
that does not work for me. which version did you try? (awk --version)
|
I'm running in version 3.1.6
|
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 |
Quote:
Code:
BEGIN{ FS ="[ .]" } Code:
Apple 5.2 20 Apple 5 2 20 |
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 |
Quote:
|
All times are GMT -5. The time now is 11:39 PM. |