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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:19:26+00:00 2026-06-18T05:19:26+00:00

I want to write a script (shell script or awk) to print $1 (first

  • 0

I want to write a script (shell script or awk) to print $1 (first field) of duplicate entry all to gather and then use the last value to find difference between last and first entry or may be note the difference of value at every duplicate entry.

For example my files has following entries:

counter1 is 100
counter2 is 200
counter3 is 300
counter1 is 1000
counter2 is 2000
counter3 is 3000
counter1 is 10000
counter2 is 20000
counter3 is 30000

I want to print:

counter1 is 100
counter1 is 1000
counter1 is 10000
counter2 is 200
counter2 is 2000
counter2 is 20000
counter3 is 300
counter3 is 3000
counter3 is 30000

Now each counter has some value incremented so I want to find difference between each value of same counter:

counter1 is 100
counter1 is 1000 | difference 1000-100 = 900
counter1 is 10000| difference 10000-100= 9900

I was able to print duplicate entries but not able to bunch them, its appearing in the same sequence as file has.

MacBook-Air:linuxscripts jimdev$ awk 'NR==FNR && a[$1]++ {b[$1];next} $1 in b' FS=" " countr.txt countr.txt 

counter1 is 100
counter2 is 200
counter3 is 300
counter1 is 1000
counter2 is 2000
counter3 is 3000
counter1 is 10000
counter2 is 20000
counter3 is 30000
  • 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-18T05:19:28+00:00Added an answer on June 18, 2026 at 5:19 am

    Assuming that your data is in a file called data.txt.
    You can get it with a sort and an awk simple if (or using patterns):

    sort data.txt | awk 'BEGIN{last = ""; value = 0;} {if ($1 == last) {print $1" is "$3" | difference "$3"-"value" = "($3-value)}else{last = $1; value = $3; print $1" is "$3;}}' -
    

    Explanation: first of all sort the input to have “counters” in ascending order. Then here we go with the AWK expression:

    1. We use 2 temporal variables: last, that stores the current counter, and the value of the first counter. We initialize it in the BEGIN part of the AWK script: BEGIN{last = ""; value = 0;}.

    Now, for each line we execute the following code:

    if ($1 == last) {
        print $1" is "$3" | difference "$3"-"value" = "($3-value);
    } else {
        last = $1;
        value = $3;
        print $1" is "$3;
    }
    

    line 1: compare the first field (counter) to last, that stores the last counter tag in order to know if we should print the difference.

    line 2: if the current line has the same counter tag than the previous one we print the difference.

    line 3: Else, it is a base case, so we save the current counter tag in order to compare with the next line, its value to calculate the difference and we print the line.

    1. If the new line has the same that same counter tag than the previous one, we keep the values and calculate the difference to the first value of this counter. Else, we store the new counter tag (in last variable) and its value (in value) and we just print the line.

    Here is the output for your input sample:

    counter1 is 100
    counter1 is 1000 | difference 1000-100 = 900
    counter1 is 10000 | difference 10000-100 = 9900
    counter2 is 200
    counter2 is 2000 | difference 2000-200 = 1800
    counter2 is 20000 | difference 20000-200 = 19800
    counter3 is 300
    counter3 is 3000 | difference 3000-300 = 2700
    counter3 is 30000 | difference 30000-300 = 29700
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a shell script that loops through all directories under a
I want to write simple script to copy/backup directory then remove on server startup.
I want to write one shell script like command1 ssh vivek@remotehost fire command on
I want to use the system command in a bash shell script. Being more
I want to write a shell script to help me start gdb and input
I want to write a shell script that spawns several long-running processes in the
I want to write a shell script ( .sh file) to get a given
I'm using vsftpd and I want to write a shell script that will detect
I want to write some thing in a file through a shell script but
I'm trying to write a shell script which recursively goes through a directory, then

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.