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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:30:15+00:00 2026-06-18T09:30:15+00:00

My command looks like this: cut -f 1 dummy_FILE | sort | uniq -c

  • 0

My command looks like this:

cut -f 1 dummy_FILE | sort | uniq -c | awk ‘{print $2}’ | for i in $(cat -); do grep -w $i dummy_FILE |
awk -v VAR=”$i” ‘{distance+=$3-$2} END {print VAR, distance}’; done

cat dummy_FILE
Red   13    14
Red   39    46
Blue   45    23
Blue   34    27
Green   31    73

I want to:
For every word in $1 dummy_FILE (Red, Blue, Green) – Calculate sum of differences between $3 and $2.

To get the output like this:

Red 8  
Blue -29  
Green 42 

My questions are:

  1. Is it possible to replace cut -f 1 dummy_FILE | sort | uniq -c | awk '{print $2}'?
    I am using sort | uniq -c to extract every word from the dataset – is it possible to do it with awk?

  2. How can I overcome useless cat in for i in $(cat -)?

  3. grep -w $i dummy_FILE works fine, but I want to replace it with awk (should I?); If so how can I do this?
    When I am trying to awk -v VAR="$i" '/^VAR/ '{distance+=$3-$2} END {print VAR, distance}' I am getting "fatal: division by zero attempted".

  • 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-18T09:30:17+00:00Added an answer on June 18, 2026 at 9:30 am

    Here’s one way using awk:

    awk '{ a[$1]=a[$1] + $3 - $2 } END { for(i in a) print i, a[i] }' dummy
    

    Results:

    Red 8
    Blue -29
    Green 42
    

    If you require sorted output, you could simply pipe into sort like arutaku suggests:

    awk '{ a[$1]=a[$1] + $3 - $2 } END { for(i in a) print i, a[i] }' dummy | sort
    

    You can however, print into sort (within the awk statement), like this:

    awk '{ a[$1]=a[$1] + $3 - $2 } END { for(i in a) print i, a[i] | "sort" }' dummy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We use grep, cut, sort, uniq, and join at the command line all the
I have a command that looks like this: ps v -p 2585 | awk
I have a Makefile that looks something like this: sometarget: command_one # calls fork()
Let's say I have an object who's class definition looks like: class Command: foo
I use Yii command line inside web Root folder (C:\xampp\htdocs\myapps\cmd.exe). My command looks like
I have a PKCS7 file, produced by M2Crypto python library, which looks like this:
I have a really simple build script that looks like this: <Project xmlns=http://schemas.microsoft.com/developer/msbuild/2003 ToolsVersion=4.0
I have a file with a list of address it looks like this (ADDRESS_FILE)
I want to run a Terminal command in my program. The command looks like
Python project looks like this: setup.py README Application scripts hello.py shell_scripts date.sh From hello.py

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.