LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2017, 03:08 AM   #1
Atulsingh2295
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Rep: Reputation: Disabled
how to multiply few columns of a file using awk


I have a file which contains these data:

1. C13 C 0.4797 0.5303 0.5447 -17.003
2. C14 C 0.4518 0.5232 0.5860 -16.271
3. C15 C 0.4667 0.4839 0.6154 -16.713
4. H1 H 0.4607 0.5484 0.6572 -16.795
I want to print column 3 as it is and multiply columns 4, 5, 6, with certain numbers and want to get output for column 4, 5, and 6 up to 6 decimal places. How can I do that?
I am able to multiply column 4, 5, 6 but not able to print column 3 using a command:

:%!awk '{printf "\%4.6f \%4.6f \%4.6f\n", $4*30, $5*30, $6*30}'
but not able to print column 3 alongwith those columns.

Last edited by Atulsingh2295; 06-22-2017 at 03:10 AM.
 
Old 06-22-2017, 03:15 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010
awk '{print $3}'
 
Old 06-22-2017, 03:23 AM   #3
Atulsingh2295
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
awk '{print $3}'
this will only print column 3. how to print other column after multiplication with numbers alongwith column 3?
 
Old 06-22-2017, 03:25 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,641

Rep: Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615
awk '{print $3 $0}'

or something similar?
 
1 members found this post helpful.
Old 06-22-2017, 03:26 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,793

Rep: Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575
The awk documentation describes how to define a string field in printf - trivial compared to floating point I would think.
 
1 members found this post helpful.
Old 06-22-2017, 03:43 AM   #6
Atulsingh2295
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
The awk documentation describes how to define a string field in printf - trivial compared to floating point I would think.

how to modify this command:
:%!awk '{printf "\%4.6f \%4.6f \%4.6f\n", $4*30, $5*30, $6*30}'

to print column 3 with this,so that it will give output like this:
C 5.5555 6.9999 7.8888
C 4.5555 6.5525 6.6666
 
Old 06-22-2017, 03:53 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,641

Rep: Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615
I still do not understand what do you need, would be better to show us an example. But you only need to modify the printf command to include what do you need.
 
Old 06-22-2017, 04:35 AM   #8
Atulsingh2295
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
I got the solution, as per your suggestion I modified the command like this......It worked. Thanks everyone.

:%! awk '{printf "\%s \%4.6f \%4.6f \%4.6f\n", $3, $4*30, $5*30, $6*30}'
 
Old 06-22-2017, 05:57 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,641

Rep: Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615Reputation: 5615
you are welcome
glad to help you
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to multiply columns that have dollar signs in them with numbers??? cyberdome Linux - Software 16 08-19-2014 03:18 PM
How to multiply columns and add them from a file? Niteawk Linux - Newbie 13 02-19-2013 01:51 AM
How can I extract columns from a file without using awk or perl? KG425 Programming 13 06-06-2012 11:40 AM
How to manipulate a range of columns in a file using awk ksvinaykumar Linux - Newbie 8 04-10-2012 09:11 AM
awk - rearrange column data in csv file to match columns wolverene13 Programming 9 12-21-2011 04:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:40 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration