Home > Archive > AWK > May 2004 > calculated field
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Matias Woloski 2004-05-04, 12:53 pm |
| Supose I have this file
001; DESC1; 0,231; 23100
002; DESC2; 0,312; 31200
001; DESC1; 0,169; 16900
I need to modify the fourth field of each record of a file, overwriting all
the values. The value 23100 is a multiplication between 0,231 and K, which
in this case is 100000.
Is awk suitable for this task? any hints?
thanks,
Matias
| |
| Barry Margolin 2004-05-04, 12:53 pm |
| In article <c773ok$emp5$1@ID-148083.news.uni-berlin.de>,
"Matias Woloski" <woloski@sion.com> wrote:
> Supose I have this file
> 001; DESC1; 0,231; 23100
> 002; DESC2; 0,312; 31200
> 001; DESC1; 0,169; 16900
>
> I need to modify the fourth field of each record of a file, overwriting all
> the values. The value 23100 is a multiplication between 0,231 and K, which
> in this case is 100000.
>
> Is awk suitable for this task? any hints?
awk '{$4 = <new value>; print}' oldfile > newfile
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Icarus Sparry 2004-05-04, 12:53 pm |
| On Tue, 04 May 2004 00:47:41 -0300, Matias Woloski wrote:
> Supose I have this file
> 001; DESC1; 0,231; 23100
> 002; DESC2; 0,312; 31200
> 001; DESC1; 0,169; 16900
>
> I need to modify the fourth field of each record of a file, overwriting all
> the values. The value 23100 is a multiplication between 0,231 and K, which
> in this case is 100000.
>
> Is awk suitable for this task? any hints?
Awk is very suitable.
awk 'BEGIN {k=100000 ;FS=";"; OFS=";"} { f3=$3 ; sub(",",".",f3) ; $4=f3*k; print}'
or
awk -F';' '{ f3=$3 ; sub(",",".",f3) ; $4=f3*100000 ; printf("%s; %s; %s; %d\n",$1,$2,$3,$4) }'
will do it. Some versions of awk may be less us-centric, and therefore
understand a "decimal comma" instead of a "decimal point", particularly if
you have the correct locale settings. If so you can avoid all the mucking
around with f3 etc and write
awk 'BEGIN {k=100000 ;FS=";"; OFS=";"} { $4=$3*k; print}'
| |
| William Park 2004-05-04, 12:53 pm |
| In <comp.lang.awk> Matias Woloski <woloski@sion.com> wrote:
> Supose I have this file
> 001; DESC1; 0,231; 23100
> 002; DESC2; 0,312; 31200
> 001; DESC1; 0,169; 16900
>
> I need to modify the fourth field of each record of a file, overwriting all
> the values. The value 23100 is a multiplication between 0,231 and K, which
> in this case is 100000.
>
> Is awk suitable for this task? any hints?
Yes. Hint:
awk '{print 0.123 * 100000}'
--
William Park, Open Geometry Consulting, <opengeometry@yahoo.ca>
Linux solution/training/migration, Thin-client
|
|
|
|
|