最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unix - shell script to sum numbers - Stack Overflow

programmeradmin1浏览0评论

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

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
Share Improve this question edited Mar 14 at 11:39 pilcrow 58.8k14 gold badges100 silver badges144 bronze badges asked Mar 14 at 9:00 VivekVivek 112 bronze badges 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. – Bodo Commented Mar 14 at 11:35
  • @Bodo , the 110 confusion was from my edit, sorry. Fixed. – pilcrow 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). – Bodo Commented Mar 14 at 12:50
Add a comment  | 

1 Answer 1

Reset to default 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
发布评论

评论列表(0)

  1. 暂无评论