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.

Pretty basic stuff. What have you tried so far ? Post your script over here.

tyler_durden

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

For the sum on the sample file given in your OP:

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

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

And yet another one:

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

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:

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

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 .

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:

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

$ 
$ # 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

Thanks Ripat, it works fine now.....but what is "C" after LANG= and why change this variable ???

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.

ok,than you...it's clearly now....and yes we use the comma as separator for the numbers.

awk '{
 if(NR%2==0)
 print $2+tmp
 else
 tmp=$2
 }' 

I only wish i was as sharp as u guys.