gawk multiply one field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting gawk multiply one field
# 1  
Old 05-28-2006
gawk multiply one field

Hi,

I am a newbie to gawk and I really don't know how to do this... I have a file with several columns of numbers and I want to make operations with some of the values...

Code:
cat file.dat | gawk -v NAME=2 '/AS/ {TIME=$4} $1==NAME {print TIME,($3*1)}'

The result of multiplying the field 3 by one yields zero, which is obviously wrong

I have tried with $3 = $3 * 1 and all possible combinations of brackets... why is it yielding zero??

thanks
# 2  
Old 05-28-2006
this works for me:
awk --version
GNU Awk 3.1.4
echo "1 2 3" | awk '{ print "mult=" $3*$2 }'

the result is zero because somehow your system sees a string instead of a number. I admit i do not see why. (asuming you have TESTED that $3 realy is a number).
# 3  
Old 05-28-2006
damn!!

Not, this gave me the same... but I have figured out the problem... the locales, man, I was employing my locales (Catalan) and all dots were substituted with commas and thus gawk could not do anything and the result of any operation was zero Smilie

So don't forget to do

Code:
LANG=C LC_ALL=C
export LANG LC_ALL

before you start gawking around
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. UNIX for Dummies Questions & Answers

Multiply value by row

Hi I would like to know how can I multiply the value of column three to columns 4-end of file example of input file: rs1000012 AK8 2 0.05 0.05 1 0 0 rs10000154 PAQR3 0.01 2 1 2 2 1 Desired output file: rs1000012 AK8 ... (1 Reply)
Discussion started by: fadista
1 Replies

3. Shell Programming and Scripting

Gawk field separator problem

I'm trying to run a code using gawk and I'm having some trouble with it. What I'm trying to do is pull out value11 from the following input: value11,value12,value13 value21,value22,value23 I have successfully done this before using awk with the following code: awk 'NR == 1 {FS=",";... (4 Replies)
Discussion started by: KomjongShawn
4 Replies

4. Windows & DOS: Issues & Discussions

Gawk on Windows: Joining lines only if 1st field matches

Hi.. i have two files:: file_1:: mOnkey huMAnfile_2:: Human:hates:banana i:like:*** Monkey:loves:banana dogs:kill:catsdesired output:: Monkey:loves:banana Human:hates:bananaso only when the 1st field matches from both files print it from file_2 ((case-sensitive)) i also would like... (21 Replies)
Discussion started by: M@LIK
21 Replies

5. Shell Programming and Scripting

multiply with awk

HI help i have cc 9+37.50 328611.50 688498.25 42.38 cc 66+62.50 328636.50 688498.42 42.58 i want to make o/p cc 9+3750 328611.50 688498.25 42.38 cc 66+6250 328636.50 688498.42 42.58 plz help (2 Replies)
Discussion started by: Indra2011
2 Replies

6. Shell Programming and Scripting

Multiply whole column with a value

Hi, I need to multiply 3rd column (comma seperated) of entire file by a value say 2.2. Suppose the file is: C,Gas $ YTD(TRI),15512.36,01/01/2010 New file should be (3rd column value multiplied by 2.2): C,Gas $ YTD(TRI),34127.192,01/01/2010 (5 Replies)
Discussion started by: yale_work
5 Replies

7. Shell Programming and Scripting

All I want to do is Multiply...

All I want to do is find 5!. read num num={1..5} while do f= done echo f Error Msg. line 5: ${1..5} bad substitution line 6: integer expression expected Line 5 is the num=... Line 6 is the "while" statement I am new at this, and I am really, really trying. Please... (14 Replies)
Discussion started by: Ccccc
14 Replies

8. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

9. Shell Programming and Scripting

Multiply variables

I have no idea about programming, just know some simple html :confused:and I need to get to somebody that can help me with creating the application (?) that will multiply 2 varibales and given price (height x lenght) x $$$. PLEASE HELP!:confused: edit by bakunin: Anita, as much as we... (2 Replies)
Discussion started by: Anita Flejter
2 Replies

10. UNIX for Dummies Questions & Answers

Calculating field using AWK, or GAWK script

Hello all, I'm totally new to UNIX/Linux but I'm taking a course in it at my local JC. My question: I have been tasked with writing a gawk script that will create a nicely formatted report. That part I've done ok on...however, the very last thing that must be done is a calculation of a... (4 Replies)
Discussion started by: Trellot
4 Replies
Login or Register to Ask a Question