Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:59:39+00:00 2026-06-05T14:59:39+00:00

I would like to write a bash code in order two compare different files

  • 0

I would like to write a bash code in order two compare different files in the following way.
When the value of the first five column are equal in both file, sum the value of the remaining column in the two different file together(e.g. colum6file1+colum6file2,etc). When they are different just keep both of the different line in the new file.
Notice that i cannot compare line by line because a line at the beginning of the first file can have the same four column value of a line at the end of the second file.

Hope this is clear,

Thanks.

File1:

  118    2   AA      4.25     30.25  1  
  119    2   AA      4.50     30.25  2  
  120    2   AA      4.75     30.25  3  
  121    2   AA      5.00     30.25  4  
  122    2   AA      5.25     30.25  5  
  123    2   AA      5.50     30.25  6    
  124    2   AA      5.75     30.25  7   
  125    2   AA      6.00     30.25  8    
  126    2   AA      6.25     30.25  9  

File2:

  179    1   BB     19.75     30.00  1  
  180    1   BB     19.75     30.00  2  
  230    1   BB     32.25     30.00  3   
  231    1   BB     32.50     30.00  4      
  232    1   BB     32.75     30.00  5   
  118    2   AA      4.25     30.25  6   
  119    2   AA      4.50     30.25  7  
  120    2   AA      5.00     30.25  8   
  121    2   AA      5.00     30.25  9   

Output:

  118    2   AA      4.25     30.25  7    
  119    2   AA      4.50     30.25  9    
  120    2   AA      4.75     30.25  11    
  121    2   AA      5.00     30.25  13    
  122    2   AA      5.25     30.25  5  
  123    2   AA      5.50     30.25  6    
  124    2   AA      5.75     30.25  7   
  125    2   AA      6.00     30.25  8    
  126    2   AA      6.25     30.25  9  
  179    1   BB     19.50     30.00  1    
  180    1   BB     19.75     30.00  2  
  230    1   BB     32.25     30.00  3   
  231    1   BB     32.50     30.00  4      
  232    1   BB     32.75     30.00  5   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:59:41+00:00Added an answer on June 5, 2026 at 2:59 pm

    This can likely be minimized, but here’s a rough cut at a solution to what I think is the question:

    sort file1 file2 |
      awk '{current = sprintf("%s %s %s %s %s", $1, $2, $3, $4, $5)
    
          if (current == previous) {
            total = total + $6
          } else if (previous) {
            printf("%s %d\n", previous, total)
            total = $6
          } else {
            total = $6
          }
    
          previous = current
        }
        END { printf("%s %d\n", previous, total) }'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write the following Bash Script: When installing LAMP like this,
I am new to bash scripting and would like to write a bash script
I would like to write the a startup script in bash to make an
I would like to write inside that code (pretty-config.xml): <pretty-config xmlns=http://ocpsoft.com/prettyfaces/3.3.2 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://ocpsoft.com/prettyfaces/3.3.2 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.2.xsd>
I would like to write some Objective-C code for distribution. What is the best
I would like to write a bash script or a few shell commands that
Let´s say I have two branches, default and mybranch, I would like to write
I would like to write a Unit Test for a (rather complex) Bash completion
I need to write a bash script , and would like it to parse
I am trying to write a bash script that looks at two files with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.