Sponsored Content
Full Discussion: extracting columns with awk
Top Forums Shell Programming and Scripting extracting columns with awk Post 302352791 by durden_tyler on Sunday 13th of September 2009 11:23:45 AM
Old 09-13-2009
Quote:
Originally Posted by achak01
...
...
Its giving one sum more could u pls explain what the scriopt is doing so i can use this knowledge to frame my own scripts later ...
If the line is blank it prints the total and resets it to 0.
Otherwise it adds the 2nd field to the running total.

My data file did not have a blank line at the end, which is the reason I added the END part in the awk script.

If your data file has a blank line at the end, then the last running total is printed on reaching that last blank line and then a 0 total is printed due to that END:

Code:
$ 
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0

sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0

$ 
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}END{printf("%s %d => %0.2f\n","Sum",++n,sum)}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
Sum 4 => 0.00
$ 
$

So, either remove the last blank line from your data file or remove the END portion of the script (but not both).

Code:
$ 
$ # 1: Correct => last blank line absent; END portion present
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0

sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0
$ 
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}END{printf("%s %d => %0.2f\n","Sum",++n,sum)}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
$ 
$ 
$ # 2: Correct => last blank line present; END portion absent
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0

sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0

$ 
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
$ 
$ 
$ # 3: Incorrect => last blank line absent; END portion absent as well
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0

sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0
$ 
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}' f2
Sum 1 => 4.83
Sum 2 => 3.00
$ 
$ 
$ # 4: Incorrect => last blank line present; END portion present as well (Your case?)
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0

sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0

$ 
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}END{printf("%s %d => %0.2f\n","Sum",++n,sum)}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
Sum 4 => 0.00
$ 
$

tyler_durden
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in extracting columns

Hi , I have a file having around 8 columns spereated by space . Now that I need to extract columns from this. The problem is this functionality is needed in a script and the required columns are dynamic and can range from 2 columns to 8 columns at a time . What I tried without luck is ... (6 Replies)
Discussion started by: praveenbvarrier
6 Replies

2. Shell Programming and Scripting

extracting multiple consecutive columns using awk

Hello, I have a matrix 200*10,000 and I need to extract the columns between 40 and 77. I dont want to write in awk all the columns. eg: awk '{print $40, $41, $42,$43 ... $77}'. I think should exist a better way to do this. (10 Replies)
Discussion started by: auratus42
10 Replies

3. Shell Programming and Scripting

Extracting columns

The output of the below command is : # yum -e0 -d0 check-update dnsmasq.i386 2.45-1.1.el5_3 updates ecryptfs-utils.i386 75-5.el5 updates fetchmail.i386 6.3.6-1.1.el5_3.1 updates... (16 Replies)
Discussion started by: proactiveaditya
16 Replies

4. Shell Programming and Scripting

Extracting only a few columns!!!?

Hi All, I have a text file which looks like below. ################################################ Name:xxxxxxx Version:1.0 Class: 2 City : Bangalore Component Part Action Nb New Part Naming Part Name 12345 default 12345.12345 Bad 23456 ... (6 Replies)
Discussion started by: smarty86
6 Replies

5. Shell Programming and Scripting

awk : extracting unique lines based on columns

Hi, snp.txt CHR_A SNP_A BP_A_st BP_A_End CHR_B BP_B SNP_B R2 p-SNP_A p-SNP_B 5 rs1988728 74904317 74904318 5 74960646 rs1427924 0.377333 0.000740085 0.013930081 5 ... (12 Replies)
Discussion started by: genehunter
12 Replies

6. UNIX for Dummies Questions & Answers

Extracting columns from multiple files with awk

hi everyone! I already posted it in scripts, I'm sorry, it's doubled I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is: awk 'NR==FNR{a=$2; next}... (1 Reply)
Discussion started by: orcaja
1 Replies

7. Shell Programming and Scripting

Extracting columns from multiple files with awk

hi everyone! I'd like to extract a single column from 5 different files and put them together in an output file. I saw a similar question for 2 input files, and the line of code workd very well, the code is: awk 'NR==FNR{a=$2; next} {print a, $2}' file1 file2 I added the file3, file4 and... (10 Replies)
Discussion started by: orcaja
10 Replies

8. Shell Programming and Scripting

AWK- extracting values from columns, saving them and gettins statistics

Hello, I am obviously quite new to unix and awk. I need to parse certain columns of a file (delimited by spaces), and somehow save the value of this column somewhere, together with the value of the column just after it (by pairs; so something like ). I'm then supposed to count the times that... (9 Replies)
Discussion started by: acsg
9 Replies

9. Shell Programming and Scripting

Extracting multiple columns with awk

Hi everyone!! I need to apply a simple command to extract columns from a matrix, but I need to extract contemporary from the first to the tenth columns, than from the eleventh to the twentyth and so on... how can i do that? (1 Reply)
Discussion started by: gabrysfe
1 Replies

10. Shell Programming and Scripting

Joining files using awk not extracting all columns from File 2

Hello All I'm joining two files using Awk by Left outer join on the file 1 File 1 1 AA 2 BB 3 CC 4 DD File 2 1 IND 100 200 300 2 AUS 400 500 600 5 USA 700 800 900 (18 Replies)
Discussion started by: venkat_reddy
18 Replies
pgmtexture(1)						      General Commands Manual						     pgmtexture(1)

NAME
pgmtexture - calculate textural features on a portable graymap SYNOPSIS
pgmtexture [-d d] [pgmfile] DESCRIPTION
Reads a portable graymap as input. Calculates textural features based on spatial dependence matrices at 0, 45, 90, and 135 degrees for a distance d (default = 1). Textural features include: (1) Angular Second Moment, (2) Contrast, (3) Correlation, (4) Variance, (5) Inverse Difference Moment, (6) Sum Average, (7) Sum Variance, (8) Sum Entropy, (9) Entropy, (10) Difference Variance, (11) Difference Entropy, (12, 13) Information Measures of Correlation, and (14) Maximal Correlation Coefficient. Algorithm taken from: Haralick, R.M., K. Shanmugam, and I. Dinstein. 1973. Textural features for image classification. IEEE Transactions on Systems, Man, and Cybertinetics, SMC-3(6):610-621. BUGS
The program can run incredibly slow for large images (larger than 64 x 64) and command line options are limited. The method for finding (14) the maximal correlation coefficient, which requires finding the second largest eigenvalue of a matrix Q, does not always converge. REFERENCES
IEEE Transactions on Systems, Man, and Cybertinetics, SMC-3(6):610-621. SEE ALSO
pgm(5), pnmcut(1) AUTHOR
Copyright (C) 1991 by Texas Agricultural Experiment Station, employer for hire of James Darrell McCauley. 22 Aug 1991 pgmtexture(1)
All times are GMT -4. The time now is 07:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy