LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-07-2012, 06:48 AM   #1
tomasdepomas
LQ Newbie
 
Registered: Jul 2012
Location: Amsterdam
Posts: 13

Rep: Reputation: 0
awk: multiply fields with constant


Hi there,

Does anyone know how I can easily multiply each field from a data file with a constant? For example using awk or something else.

cheers,
Tomas
 
Old 12-07-2012, 06:51 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Is this what you are looking for:
Code:
$ cat infile
2
3
4

$ awk '{ print $1, "times 5 is:", $1*5 }' infile
2 times 5 is: 10
3 times 5 is: 15
4 times 5 is: 20
 
Old 12-07-2012, 06:55 AM   #3
tomasdepomas
LQ Newbie
 
Registered: Jul 2012
Location: Amsterdam
Posts: 13

Original Poster
Rep: Reputation: 0
The data files have about a 1000 rows and columns, but this is different for each of them. So how can I easily do the multiplication for all fields? thanks
 
Old 12-07-2012, 07:03 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Code:
$ cat infile
2 3 4
3 4 5 6
4 5 6 7 8

$ awk '{ for ( i=1; i<=NF; i++ ) printf $i*5" " }{ print"" }' infile
10 15 20 
15 20 25 30 
20 25 30 35 40
If this still isn't what you want/need: Please post a sample of your input and wanted output.
 
1 members found this post helpful.
Old 12-07-2012, 07:04 AM   #5
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
Are you expecting the o/p like this?
[bijo]$ echo 2 3 4 5 | awk -v x=4 -F " " '{print x*$1,x*$2, x*$3, x*$4 }'
8 12 16 20
--
Thanks,
Bijo
 
Old 12-07-2012, 07:07 AM   #6
tomasdepomas
LQ Newbie
 
Registered: Jul 2012
Location: Amsterdam
Posts: 13

Original Poster
Rep: Reputation: 0
That works great , thank you very much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
[SOLVED] get fields using awk ashok.g Programming 9 12-09-2009 01:21 AM
[awk] Metacharacters in string constant Pico1 Programming 4 04-26-2009 04:06 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Supressing Fields w/ AWK Rv5 Programming 3 10-19-2004 11:06 AM


All times are GMT -5. The time now is 05:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration