extracting columns with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting columns with awk
# 1  
Old 09-13-2009
extracting columns with awk

Friends, I have a file with fileds in the following order

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 0.00 0.00 0.00 0 0

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

Now what i want is to sum up the values in 2nd column 2 rows at a time like first sum will be 4.80 + .03. 2nd will be 1.00 + 0.00. and so on. Pls can u advice me how i can acheive this.
# 2  
Old 09-13-2009
Pretty basic stuff. What have you tried so far ? Post your script over here.

tyler_durden
# 3  
Old 09-13-2009
This is the script i developed. pls help me in correcting it out as i am new to awk.

i =`iostat -dt | egrep -v "Linux|Device|Time" | cut -d" " -f1 |wc -l`i=`expr $i - 1`
j=$i
echo $i
echo "enter Iterations"
read iterations
echo "enter interval"
read interval
iostat -dt $interval $iterations | egrep -v "Linux|Device|Time" > file





for ((a=1;a<=$iterations;a++))
do
head -$i file | awk ' { s += $2 } { values["'$a'"]=s } END { print "Sum", values["'$a'"]}'
i=`expr $i + $j` done

Last edited by achak01; 09-13-2009 at 04:55 AM..
# 4  
Old 09-13-2009
For the sum on the sample file given in your OP:

Code:
awk '{t+=$2}!((NR+1)%3){printf"%.2f\n",t;t=0}' file

But by guessing what you are trying to do in the above script, I think you can simplify it a lot. At least in the second part, after the user input. Piping head into awk is useless. awk can easily emulate head. Give us more input on file to process, like a typical output of command iostat that I don't have on my linux box.

NB: Please use the code tag (the # in your edit window).
# 5  
Old 09-13-2009
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 0.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 0.00 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 => 1.00
Sum 3 => 1.00
$

tyler_durden
# 6  
Old 09-13-2009
And yet another one:

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

# 7  
Old 09-13-2009
Your solutions (on my MacBook) displayed on the first line 4,00 and not 4,83 ???
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question