## Combine Values from Rows

### Combine Values from Rows

Hello.
I have a general question on combining numeric values between multiple
lines within a flat-file.

For example:

\$ cat test.file

123 345 567 789
987 654 432 123
543 876 098 123
324 657 980 501
987 654 432 123
543 876 098 123

So what I want to do with the data above is combine (add) the numeric
values of position 1-4 with the number directly below in the same
position. I want to do this with every other line. So I want to combine
line1 with line2 and line3 with line4 and line5 with line6, etc.

To clarify more, I want the end result to look like below:

line1_position1 + line2_position1, line1_position2 + line2_position2 \
line1_position3 + line2_position3, line1_position4 + line2_position4
line3_position1 + line4_position1, line3_position2 + line4_position2 \
line3_position3 + line4_position3, line3_position4 + line4_position4
...
...

Thanks for any help!
MA

### Combine Values from Rows

2005-01-26, 11:54(-05), MA Fryler:
[...]
Quote:> \$ cat test.file

> 123 345 567 789
> 987 654 432 123
> 543 876 098 123
> 324 657 980 501
> 987 654 432 123
> 543 876 098 123

> So what I want to do with the data above is combine (add) the numeric
> values of position 1-4 with the number directly below in the same
> position. I want to do this with every other line. So I want to combine
> line1 with line2 and line3 with line4 and line5 with line6, etc.

> To clarify more, I want the end result to look like below:

> line1_position1 + line2_position1, line1_position2 + line2_position2 \
> line1_position3 + line2_position3, line1_position4 + line2_position4
> line3_position1 + line4_position1, line3_position2 + line4_position2 \
> line3_position3 + line4_position3, line3_position4 + line4_position4

[...]

Something like:

awk '
{
a = \$1; b = \$2; c = \$3; d = \$4
if ((getline) > 0)
print a + \$1, b + \$2, c + \$3, d + \$4
}' < test.file

--
Stphane

### Combine Values from Rows

> Something like:

> awk '
>   {
>     a = \$1; b = \$2; c = \$3; d = \$4
>     if ((getline) > 0)
>       print a + \$1, b + \$2, c + \$3, d + \$4
>   }' < test.file

Thank you. That is exactly what I am looking for. The 'getline' piece
was what I was missing.

MA

### Combine Values from Rows

>>Something like:

>>awk '
>>  {
>>    a = \$1; b = \$2; c = \$3; d = \$4
>>    if ((getline) > 0)
>>      print a + \$1, b + \$2, c + \$3, d + \$4
>>  }' < test.file

> Thank you. That is exactly what I am looking for. The 'getline' piece
> was what I was missing.

getline is rarely required or desirable in an awk script. This is more
idiomatic:

awk 'NR%2{for(i=1;i<=NF;i++)p[i]=\$i;next}
{for(i=1;i<=NF;i++)\$i=\$i+p[i]}1' test.file

If your fields are all separated by a single space as in your example,
it can be slightly simpler:

awk 'NR%2{split(\$0,p);next}
{for(i=1;i<=NF;i++)\$i=\$i+p[i]}1' test.file

You can pass split() a third argument to define your separation fields
if you care.

It won't work with old awk, but you shouldn't use that anyway.

Regards,

Ed.

### Combine Values from Rows

> I have a general question on combining numeric values between multiple
> lines within a flat-file.

> For example:

> \$ cat test.file

> 123 345 567 789
> 987 654 432 123
> 543 876 098 123
> 324 657 980 501
> 987 654 432 123
> 543 876 098 123

> So what I want to do with the data above is combine (add) the numeric
> values of position 1-4 with the number directly below in the same
> position. I want to do this with every other line. So I want to combine
> line1 with line2 and line3 with line4 and line5 with line6, etc.

> To clarify more, I want the end result to look like below:

> line1_position1 + line2_position1, line1_position2 + line2_position2 \
> line1_position3 + line2_position3, line1_position4 + line2_position4
> line3_position1 + line4_position1, line3_position2 + line4_position2 \
> line3_position3 + line4_position3, line3_position4 + line4_position4

\$ cat test-Fryler
123 345 567 789
987 654 432 123
543 876 098 123
324 657 980 501
987 654 432 123
543 876 098 123

1110 999 999 912
867 1533 1078 624
1530 1530 530 246

John
--
use Perl;
program
fulfillment

Dear Group,

I have the following file.

\$ cat my_input.txt

Group Id 6a -- 10 hrs

3         2       1114313           Fixed    0
3         4       1114296           Fixed    0

Group Id 7a -- 10 hrs

3         2       1114313           Fixed    0
3         4       1114296           Fixed    0
3         5       1014200           Fixed    0

I would like to take the number following the "Group Id' and stick the
value in fron of the following lines, untill another "Group Id" is met.

i.e. The output file has te be in the following format.

6a 3         2       1114313           Fixed    0
6a 3         4       1114296           Fixed    0

7a 3         2       1114313           Fixed    0
7a 3         4       1114296           Fixed    0
7a 3         5       1014200           Fixed    0

Could the above be achieved using some perl/awk or sed script? If so,
can you please give me some guidence?

Thanks!!!