1

I have the following data:

KEY v2025.10: user1 0/2 at 03/06 14:00  (handle: e01)
KEY v2025.10: user1 1/2 at 03/06 14:01  (handle: f01)
KEY v2025.10: user2 0/1 at 03/06 14:01  (handle: 1001)
KEY v2025.10: user3 1/0 at 03/06 14:01  (handle: 1081)
KEY v2025.10: user4 1/100 at 03/06 14:02  (handle: 11c1)
KEY v2025.10: user5 1/1 at 03/06 14:02  (handle: 1201)

The relevant fields of the data are: KEY v2025.10 $user $unreserved/$reserved at.... I am trying to sum the $unreserved and $reserved counts by $user to get for example:

user1 unreserved =1
user1 reserved =4
user2 unreserved =0
user2 reserved =1
user3 unreserved =1
user3 reserved =0

and so on. I also need to get the total sum of both $unreserved and $reserved counts together, in this case 110.

I tried like below;

grep handle | awk -F' ' '$1!=p{ if (NR>1) print p, s; p=$1; s=0} {s+=$12} END{print p, s}' | sort | uniq -c | sort -n
3
  • Please edit your question and show the output of your command. I don't see an attempt to print "unreserved" or "reserved". Why do you check (NR>1)? Does your real input have a header line that is not shown in the question? Please clarify in your question: How do you calculate the number 110? Is it the total sum of all reserved and unreseved numbers? Please also check if the edit matches your intention and fix it if necessary. Commented Mar 14 at 11:35
  • @Bodo , the 110 confusion was from my edit, sorry. Fixed. Commented Mar 14 at 11:40
  • @pilcrow Your edit matches my interpretation now, and I have seen that your attempted clarification led to the inconsistency. Anyway it would be good if the OP would confirm that your edit matches the intention of the question. @Vivek: Your command prints 1 KEY 0 (indented by 6 spaces). Commented Mar 14 at 12:50

1 Answer 1

0

pure awk solution:

awk '/handle/ {
  u = $3;             # user in field 3
  n12 = $4;           # numbers 1 and 2 in field 4
  split(n12 ,n, "/"); # split numbers separated by slash into array
  un[u] += n[1];      # add to unreserved for user
  re[u] += n[2];      # add to reserved for user
  s += n[1]+n[2];     # add to total sum
}
END {
  for(i in re)        # loop over all associative array indices
  {
    # both arrays have the same index values
    printf "%s unreserved =%d\n", i, un[i];
    printf "%s reserved =%d\n", i, re[i];
  }
  printf "total =%d\n",s;
}'

(Of course you can omit all comments and put everything into a single line.)
You can feed the input into stdin or add a file name as a command line argument.

With your example input I get this output:

user1 unreserved =1
user1 reserved =4
user2 unreserved =0
user2 reserved =1
user3 unreserved =1
user3 reserved =0
user4 unreserved =1
user4 reserved =100
user5 unreserved =1
user5 reserved =1
total =110
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.