multiplying column based on matching of second column in bash
I have two files. The first file has total number of machines with particuar cpu type. It has two columns. This is how it looks.
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
The second file has the cost/month for each cpu type. It has this format:
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
To caluclate the overall effective cost , I need to take each variable from the second column of the first file and multiply it with its corresponding price from the second file.
once this is acheived , we need to add the individual prices to arrive at a total cost .
bash shell scripting multiple-columns text-processing
add a comment |
I have two files. The first file has total number of machines with particuar cpu type. It has two columns. This is how it looks.
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
The second file has the cost/month for each cpu type. It has this format:
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
To caluclate the overall effective cost , I need to take each variable from the second column of the first file and multiply it with its corresponding price from the second file.
once this is acheived , we need to add the individual prices to arrive at a total cost .
bash shell scripting multiple-columns text-processing
3
Cool. What have you tried? What problem do you have? What is the output you have? What is expected output you want to have? You can find someone who will do the job for you at freelancing sites. This looks pretty simple - use join to merge files and datamesh or awk to calculate. You can read about How do I ask a good question? and question checklist.
– Kamil Cuk
Nov 13 '18 at 14:09
May I recommend a database?
– Paul Hodges
Nov 13 '18 at 16:13
add a comment |
I have two files. The first file has total number of machines with particuar cpu type. It has two columns. This is how it looks.
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
The second file has the cost/month for each cpu type. It has this format:
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
To caluclate the overall effective cost , I need to take each variable from the second column of the first file and multiply it with its corresponding price from the second file.
once this is acheived , we need to add the individual prices to arrive at a total cost .
bash shell scripting multiple-columns text-processing
I have two files. The first file has total number of machines with particuar cpu type. It has two columns. This is how it looks.
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
The second file has the cost/month for each cpu type. It has this format:
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
To caluclate the overall effective cost , I need to take each variable from the second column of the first file and multiply it with its corresponding price from the second file.
once this is acheived , we need to add the individual prices to arrive at a total cost .
bash shell scripting multiple-columns text-processing
bash shell scripting multiple-columns text-processing
asked Nov 13 '18 at 14:06
santhusanthu
24
24
3
Cool. What have you tried? What problem do you have? What is the output you have? What is expected output you want to have? You can find someone who will do the job for you at freelancing sites. This looks pretty simple - use join to merge files and datamesh or awk to calculate. You can read about How do I ask a good question? and question checklist.
– Kamil Cuk
Nov 13 '18 at 14:09
May I recommend a database?
– Paul Hodges
Nov 13 '18 at 16:13
add a comment |
3
Cool. What have you tried? What problem do you have? What is the output you have? What is expected output you want to have? You can find someone who will do the job for you at freelancing sites. This looks pretty simple - use join to merge files and datamesh or awk to calculate. You can read about How do I ask a good question? and question checklist.
– Kamil Cuk
Nov 13 '18 at 14:09
May I recommend a database?
– Paul Hodges
Nov 13 '18 at 16:13
3
3
Cool. What have you tried? What problem do you have? What is the output you have? What is expected output you want to have? You can find someone who will do the job for you at freelancing sites. This looks pretty simple - use join to merge files and datamesh or awk to calculate. You can read about How do I ask a good question? and question checklist.
– Kamil Cuk
Nov 13 '18 at 14:09
Cool. What have you tried? What problem do you have? What is the output you have? What is expected output you want to have? You can find someone who will do the job for you at freelancing sites. This looks pretty simple - use join to merge files and datamesh or awk to calculate. You can read about How do I ask a good question? and question checklist.
– Kamil Cuk
Nov 13 '18 at 14:09
May I recommend a database?
– Paul Hodges
Nov 13 '18 at 16:13
May I recommend a database?
– Paul Hodges
Nov 13 '18 at 16:13
add a comment |
2 Answers
2
active
oldest
votes
awk 'FNR==NRcost[$1]=$2;nextsum=$1*cost[$2];print $2,sum;total+=sumENDprint total' file2 file1
When you post next time, please show what part of your code your are having issues from.
– ssemilla
Nov 13 '18 at 14:55
add a comment |
Using Perl Oneliner
> cat santhu1
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
> cat santhu2
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
> perl -lane 'BEGIN %kv=map split(/s+/),qx(cat santhu2) print $F[0]*$kv$F[1]," ",$F[1] ' santhu1
265 custom
1740 n1-highcpu-32
216 n1-highcpu-4
5324 n1-highmem-16
425 n1-standard-1
3492 n1-standard-16
1323 n1-standard-2
1261 n1-standard-4
970 n1-standard-8
>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282821%2fmultiplying-column-based-on-matching-of-second-column-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
awk 'FNR==NRcost[$1]=$2;nextsum=$1*cost[$2];print $2,sum;total+=sumENDprint total' file2 file1
When you post next time, please show what part of your code your are having issues from.
– ssemilla
Nov 13 '18 at 14:55
add a comment |
awk 'FNR==NRcost[$1]=$2;nextsum=$1*cost[$2];print $2,sum;total+=sumENDprint total' file2 file1
When you post next time, please show what part of your code your are having issues from.
– ssemilla
Nov 13 '18 at 14:55
add a comment |
awk 'FNR==NRcost[$1]=$2;nextsum=$1*cost[$2];print $2,sum;total+=sumENDprint total' file2 file1
awk 'FNR==NRcost[$1]=$2;nextsum=$1*cost[$2];print $2,sum;total+=sumENDprint total' file2 file1
answered Nov 13 '18 at 14:53
ssemillassemilla
3,087424
3,087424
When you post next time, please show what part of your code your are having issues from.
– ssemilla
Nov 13 '18 at 14:55
add a comment |
When you post next time, please show what part of your code your are having issues from.
– ssemilla
Nov 13 '18 at 14:55
When you post next time, please show what part of your code your are having issues from.
– ssemilla
Nov 13 '18 at 14:55
When you post next time, please show what part of your code your are having issues from.
– ssemilla
Nov 13 '18 at 14:55
add a comment |
Using Perl Oneliner
> cat santhu1
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
> cat santhu2
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
> perl -lane 'BEGIN %kv=map split(/s+/),qx(cat santhu2) print $F[0]*$kv$F[1]," ",$F[1] ' santhu1
265 custom
1740 n1-highcpu-32
216 n1-highcpu-4
5324 n1-highmem-16
425 n1-standard-1
3492 n1-standard-16
1323 n1-standard-2
1261 n1-standard-4
970 n1-standard-8
>
add a comment |
Using Perl Oneliner
> cat santhu1
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
> cat santhu2
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
> perl -lane 'BEGIN %kv=map split(/s+/),qx(cat santhu2) print $F[0]*$kv$F[1]," ",$F[1] ' santhu1
265 custom
1740 n1-highcpu-32
216 n1-highcpu-4
5324 n1-highmem-16
425 n1-standard-1
3492 n1-standard-16
1323 n1-standard-2
1261 n1-standard-4
970 n1-standard-8
>
add a comment |
Using Perl Oneliner
> cat santhu1
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
> cat santhu2
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
> perl -lane 'BEGIN %kv=map split(/s+/),qx(cat santhu2) print $F[0]*$kv$F[1]," ",$F[1] ' santhu1
265 custom
1740 n1-highcpu-32
216 n1-highcpu-4
5324 n1-highmem-16
425 n1-standard-1
3492 n1-standard-16
1323 n1-standard-2
1261 n1-standard-4
970 n1-standard-8
>
Using Perl Oneliner
> cat santhu1
5 custom
3 n1-highcpu-32
3 n1-highcpu-4
11 n1-highmem-16
17 n1-standard-1
9 n1-standard-16
27 n1-standard-2
13 n1-standard-4
5 n1-standard-8
> cat santhu2
n1-standard-1 25
n1-standard-2 49
n1-standard-4 97
n1-standard-8 194
n1-standard-16 388
n1-standard-32 777
n1-standard-64 1553
n1-highmem-2 60
n1-highmem-4 121
n1-highmem-8 242
n1-highmem-16 484
n1-highcpu-4 72
n1-highcpu-32 580
custom 53
> perl -lane 'BEGIN %kv=map split(/s+/),qx(cat santhu2) print $F[0]*$kv$F[1]," ",$F[1] ' santhu1
265 custom
1740 n1-highcpu-32
216 n1-highcpu-4
5324 n1-highmem-16
425 n1-standard-1
3492 n1-standard-16
1323 n1-standard-2
1261 n1-standard-4
970 n1-standard-8
>
answered Nov 13 '18 at 15:44
stack0114106stack0114106
2,7461417
2,7461417
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282821%2fmultiplying-column-based-on-matching-of-second-column-in-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Cool. What have you tried? What problem do you have? What is the output you have? What is expected output you want to have? You can find someone who will do the job for you at freelancing sites. This looks pretty simple - use join to merge files and datamesh or awk to calculate. You can read about How do I ask a good question? and question checklist.
– Kamil Cuk
Nov 13 '18 at 14:09
May I recommend a database?
– Paul Hodges
Nov 13 '18 at 16:13