Multiply certain column to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiply certain column to variable
# 1  
Old 12-05-2011
Multiply certain column to variable

Hi experts,
I want to multiply certain columns to variable,
data :
Code:
1 2 3 4 5 6 7 8 9

result with var = 2 for column 3,6,9 ... (every columns which can be divided to 3):
Code:
1 2 6 4 5 12 7 8 18

I have tried :
Code:
awk 'BEGIN{FS=OFS=" "}{print $1,$2,$3*a,$4,$5,$6*a,$7,$8,$9*2 }' a=2 file.txt

but how can I do it automatically (because I have like 1000 columns),
not manually by typing the columns no ? Smilie

Thanks for the help.
# 2  
Old 12-05-2011
Code:
perl -e '$/=" ";open I,"< inputfile";$i=1;for(<I>){($i%3==0)?print $_*2 ." ":print $_;$i++};close I'

Is it in anyway related to the below post?
https://www.unix.com/shell-programmin...olumn-awk.html
# 3  
Old 12-05-2011
that post was another case,
would it be possible to find the solution in awk,
I am totally new-by in perl, .. Smilie
# 4  
Old 12-05-2011
Code:
echo "1 2 3 4 5 6 7 9 12" | awk -v var=2 '{for(i=3;i<=NF;i=i+3) {$i= $i*var}  }1'

This User Gave Thanks to tarun_agrawal For This Post:
# 5  
Old 12-05-2011
Try this..
Code:
awk '{for(i=1;i<=NF;i++){printf (i%3?i:i*var)OFS}}' var=2 input_file

--ahamed
# 6  
Old 12-05-2011
@tarun : Thanks, it works !!
@ahamed : it seems the command did not write the data, but the columns no.. thanks anyway
# 7  
Old 03-07-2012
Thread hijack moved to new thread under programming:
Multiplying column in awk and PHP
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiply values from a file

Hi All, I extracted a file using an awk command like this awk '{print substr($0,78,5)"," substr($0,59,6) "," substr($0,81,3) "," substr($0,11,7)}' file1 >> OUTPUT cat OUTPUT 00001,100000,005,0000080 00001,100000,008,0000220 00001,100000,001,0001000 00010,100000,001,0000400 I want... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. UNIX for Beginners Questions & Answers

Add column and multiply its result to all elements of another column

Input file is as follows: 1 | 6 2 | 7 3 | 8 4 | 9 5 | 10 Output reuired (sum of the first column $1*$2) 1 | 6 | 90 2 | 7 | 105 3 | 8 | 120 4 |9 | 135 5 |10 | 150 Please enclose sample input, sample output, and code... (5 Replies)
Discussion started by: Sagar Singh
5 Replies

3. 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

4. Shell Programming and Scripting

How to search, replace and multiply variable within awk?

I have a file that reports the size of disks GB's or TB's - I need the file to report everything in MB's. Here is an extract of the file - the last column is the disk size. 19BC 2363 20G 1AA3 2363 2.93T 1A94 2363 750G Whenever I come across a G I want to delete the G and multiply by... (2 Replies)
Discussion started by: kieranfoley
2 Replies

5. Shell Programming and Scripting

Need to extract data from Column having variable length column

Hi , I need to extract data from below mentioned data, having no delimiter and havin no fixed column length. For example: Member nbr Ref no date 10000 1000 10202012 200000 2000 11202012 Output: to update DB with memeber nbr on basis of ref no. ... (6 Replies)
Discussion started by: ns64110
6 Replies

6. 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

7. 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

8. 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

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. Shell Programming and Scripting

multiply variable

I need to multiply the value of a variable and then store it in another variable. I have EXPHOURINSEC=$(($EXPDATEHOUR * 3600)) but i get an error saying the * is unexpected. Im using ksh (4 Replies)
Discussion started by: fwabbly
4 Replies
Login or Register to Ask a Question