awk file sum results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk file sum results
# 1  
Old 09-26-2014
awk file sum results

I ran this script:
Code:
#!/bin/bash
./disk-usage-JM.pl > jm_out
InFile="jm_out"
OutFile="jm_temp"
awk '{if (!match(" \t",substr($0,1,1))) {server=$0; next}
      if (substr($0,1,1)==" ") {workspace=$0; next}
        print server,workspace,$0}' \
  $InFile >$OutFile
rm jm_out

against this InFile: (truncated)
Code:
adallman:
  sideshow:
	bob               12065 MB
	mel                 488 MB
  simpsons:
	bart              32965 MB
afkham:
  simpsons:
	lisa             102466 MB
agnewjo:
  flanders:
	ned               70847 MB
	rod                2657 MB
ahoang:
  flanders:
	rod                2896 MB
akrishna:
  flanders:
	ned                3310 MB
  moes:
	barney             1850 MB
	carl              15674 MB
	lenny             10723 MB
  sideshow:
	bob                   0 MB
	mel              101700 MB
  simpsons:
	bart                  0 MB
	lisa                  0 MB
alexu (LOCKED):
  flanders:
	maude              4041 MB
	ned                5011 MB
  simpsons:
	bart               1326 MB
	marge              1855 MB
alin:
  moes:
	lenny               272 MB
alindema:
  moes:
	barney                0 MB
  sideshow:
	bob                   0 MB
  simpsons:

and I only got:
Code:
zzhu (LOCKED):     flanders-ws1       4857 MB

I was supposed to get:
Code:
adallman:   sideshow: 	bob               12065 MB
adallman:   sideshow: 	mel                 488 MB
adallman:   simpsons: 	bart              32965 MB
afkham:   simpsons: 	lisa             102466 MB
agnewjo:   flanders: 	ned               70847 MB
agnewjo:   flanders: 	rod                2657 MB
ahoang:   flanders: 	rod                2896 MB
akrishna:   flanders: 	ned                3310 MB
akrishna:   moes: 	barney             1850 MB
akrishna:   moes: 	carl              15674 MB
akrishna:   moes: 	lenny             10723 MB
akrishna:   sideshow: 	bob                   0 MB
akrishna:   sideshow: 	mel              101700 MB
akrishna:   simpsons: 	bart                  0 MB
akrishna:   simpsons: 	lisa                  0 MB
alexu (LOCKED):   flanders: 	maude              4041 MB
alexu (LOCKED):   flanders: 	ned                5011 MB
alexu (LOCKED):   simpsons: 	bart               1326 MB
alexu (LOCKED):   simpsons: 	marge              1855 MB
alin:   moes: 	lenny               272 MB
alindema:   moes: 	barney                0 MB
alindema:   sideshow: 	bob                   0 MB

Here's how it ran:
Code:
+ ./disk-usage-JM.pl
+ InFile=jm_out
+ OutFile=jm_temp
+ awk '{if (!match(" \t",substr($0,1,1))) {server=$0; next}
      if (substr($0,1,1)==" ") {workspace=$0; next}
        print server,workspace,$0}' jm_out
+ rm jm_out

Perhaps I need to loop it?

I'm trying to total the usage by user per workspace. The organization of the InFile is user left most, server second, workspace third, size fourth.

The 4 servers are:

Code:
sideshow
simpsons
moes
flanders

So we could go:

Code:
SERVERS="sideshow simpsons moes flanders"

And we could do this for the workspaces:

Code:
case $SERVERS in
sideshow) WORKSPACES=(bob mel sideshow-ws2) ;;
simpsons) WORKSPACES=bart homer lisa marge releases rt-private simpsons-ws0 simpsons-ws1 simpsons-ws2 vsimpsons-ws) ;;
moes)     WORKSPACES=(barney carl lenny moes-ws2) ;;
flanders) WORKSPACES2=(flanders-ws0 flanders-ws1 flanders-ws2 maude ned rod todd to-delete) ;;


Last edited by master-of-puppe; 09-26-2014 at 01:32 AM..
# 2  
Old 09-26-2014
I tried 3 different awks and got the output that you are looking for. What OS and version are you using and what awk?
# 3  
Old 09-26-2014
Quote:
Originally Posted by Scrutinizer
I tried 3 different awks and got the output that you are looking for. What OS and version are you using and what awk?
I'm running this on debian wheezy or squeeze either one.

This is squeeze:

Code:
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 6.0.7 (squeeze)
Release:        6.0.7
Codename:       squeeze

Code:
GNU Awk 3.1.7

This is wheezy:

Code:
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 7.3 (wheezy)
Release:        7.3
Codename:       wheezy

Code:
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF             32767
sprintf buffer      2040


Last edited by master-of-puppe; 09-26-2014 at 02:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sum value of a column by range defined in another file awk?

I have two files, file1.table is the count table, and the other is the range condition file2.range. file1.table chr start end count N1 0 48 1 N1 48 181 2 N1 181 193 0 N1 193 326 2 N1 326 457 0 N1 457 471 1 N1 471 590 2 N1 590 604 1 N1 604 752 1 N1 752 875 1 file2.range... (12 Replies)
Discussion started by: yifangt
12 Replies

2. UNIX for Advanced & Expert Users

Need Optimization shell/awk script to aggreagte (sum) for all the columns of Huge data file

Optimization shell/awk script to aggregate (sum) for all the columns of Huge data file File delimiter "|" Need to have Sum of all columns, with column number : aggregation (summation) for each column File not having the header Like below - Column 1 "Total Column 2 : "Total ... ...... (2 Replies)
Discussion started by: kartikirans
2 Replies

3. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

4. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Sum duplicate values in text file through awk between dates

I need to sum values in text file in case duplicate row are present with same name and different value below is example of data in file i have and format i need. Data in text file 20170308 PM,U,2 PM,U,113 PM,I,123 DA,U,135 DA,I,113 DA,I,1 20170309 PM,U,2 PM,U,1 PM,I,123 PM,I,1... (3 Replies)
Discussion started by: Adfire
3 Replies

6. Shell Programming and Scripting

Append awk results into file or array

for a in {1..100} do awk '{ sum+=$a} END {print sum}' a=$a file1 > file2 done I know I will get only one number if following the code above, how can I get 100 sum numbers in file2? (2 Replies)
Discussion started by: wanliushao
2 Replies

7. Shell Programming and Scripting

Sum of 286th column using awk in a file

Hi, I am using the following code to find the sum of the values of column 286 in a file. It will have the Decimal values with the scale of 2. Delimiter is '|^' cut -d'|^' -f286 filename|cut -c3-| awk '{ x += $1 } END { printf("%.2f\n", x) }' There are around 50k records in this file... (2 Replies)
Discussion started by: Jram
2 Replies

8. Shell Programming and Scripting

awk help: Match data fields from 2 files & output results from both into 1 file

I need to take 2 input files and create 1 output based on matches from each file. I am looking to match field #1 in both files (Userid) and create an output file that will be a combination of fields from both file1 and file2 if there are any differences in the fields 2,3,4,5,or 6. Below is an... (5 Replies)
Discussion started by: ambroze
5 Replies

9. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

10. Shell Programming and Scripting

Sum of all lines in file without roundup with awk

Hi, I have a file and I want to sum all the numbers in it. Example of the file: 0.6714359 -3842.59553830551 I used your forum (https://www.unix.com/shell-programming-scripting/74293-how-get-sum-all-lines-file.html) and found a script, what worked for me: awk '{a+=$0}END{print a}'... (8 Replies)
Discussion started by: mario8eren
8 Replies
Login or Register to Ask a Question