Visit Our UNIX and Linux User Community


extracting columns with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting columns with awk
# 8  
Old 09-13-2009
Quote:
Originally Posted by protocomm
Your solutions (on my MacBook) displayed on the first line 4,00 and not 4,83 ???
It is probably because your are on french locale. Try:

Code:
LANG=C awk -F" |\n" -v RS="" '{print $2+$8}' file

# 9  
Old 09-13-2009
Thanks duden. u script worked like charm. 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 .
# 10  
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
# 11  
Old 09-13-2009
Thanks Ripat, it works fine now.....but what is "C" after LANG= and why change this variable ???
# 12  
Old 09-13-2009
That environment shell variable defines the localisation you want. As the OP uses the dot as decimal separator and you - as Frenchman - are supposedly using the comma in your locale, you need to temporarily force that variable to C (POSIX) or any other locale that use the dot as decimal separator and the comma as thousand grouping character like all us and uk locales.

You could also have used LC_ALL or the more restrictive LC_NUMERIC to the same effect.
# 13  
Old 09-13-2009
ok,than you...it's clearly now....and yes we use the comma as separator for the numbers.
# 14  
Old 09-14-2009
Code:
awk '{
 if(NR%2==0)
 print $2+tmp
 else
 tmp=$2
 }'


Previous Thread | Next Thread
Test Your Knowledge in Computers #540
Difficulty: Medium
In dynamically typed programming languages. instead of declaring a variable to have a particular type, the type of a variable is determined by an A.I. in the operating system.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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

Featured Tech Videos