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 6749493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:41:48+00:00 2026-05-26T12:41:48+00:00

i have a dataset which is like the following, however much larger: 5 6

  • 0

i have a dataset which is like the following, however much larger:

5 6 9
2 4 6
4 5 1

I want to be able to subtract every field in each record from the current one, then add them together and store the results. For example here i would want, starting with the first line (5-2) + (6-4) + (9-6) = result. And also (5-4) + (6-5) and (9-1). Also do this for all other lines aswell, for example for second line (2-4) + (4-5) + (6-1) and (2-5) + (4-6) + (6-9) etc. I can do this manually as follows:

{
     if (max_nf < NF)
          max_nf = NF
     max_nr = NR
     for (x = 1; x <= NF; x++)
          vector[x, NR] = $x
}

END { result = ((vector[1,1] - vector[1,2]) + (vector[2,1] - vector[2,2]) + (vector[3,1] - vector[3,2]))
}

however the dataset is large, and i would like a loop to do this, which i cannot seem to get working.

  • 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-05-26T12:41:49+00:00Added an answer on May 26, 2026 at 12:41 pm

    One obvious possibility would be something on this order:

    BEGIN { getline; fields = NF+1; for (i=1; i<fields; i++) first[i] = $i; }
    
        { 
          total = 0;
          for (field = 1; field < fields; field++)  
              total += first[field] - $field;
          printf("%d\n", total);
        }
    

    Note that this makes no attempt at detecting or dealing intelligently with bad input.

    Edit (to fit edited question):

    Given what you’ve changed the question to ask, you apparently need to read all the data into an array, then walk through the array and add up the differences between the records. This no longer really fits very well with how awk works, so my immediate advice would be to use something else. If you insist on using awk anyway, you could basically put all the processing into the BEGIN block, reading all the lines into a big array, then walking through it, doing all the math, and then printing the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample dataset (below and/or as CSVs here: http://goo.gl/wK57T ) which
I have the following XML document which I would like to parse into a
I have a data set which looks something like data<-c(0,1,2,3,4,2,3,1,4,3,2,4,0,1,2,0,2,1,2,0,4) frame<-as.data.frame(data) I now want
Working in python I want to extract a dataset with the following structure: Each
I have a dataset of 2D points (~500k of them) on which I'd like
I have DataSet which has 3 columns. Name - insurance comp. name - treatmentDate
(Using Visual Studio 2005 / .NET 2.0) I have a DataSet which is being
I have a 2-class dataset on which I should apply a binary classification algorithm.
I have created a dataset in my visual studio project which is connected to
I have a moderately sized dataset in excel from which I wish to extract

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.