Combine Values from Rows

Combine Values from Rows

Post by MA Fryle » Fri, 28 Jan 2005 01:54:07



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

Post by Stephane CHAZELA » Fri, 28 Jan 2005 02:14:15


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

Post by MA Fryle » Fri, 28 Jan 2005 03:28:13



> 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

Post by Ed Morto » Fri, 28 Jan 2005 15:26:41




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

Post by John W. Krah » Fri, 28 Jan 2005 17:57:30



> 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

 
 
 

1. Using awk or perl or sed to format/combine rows

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

2. Routing Problem

3. ksh: finding the value of a value in a for loop

4. CD rom Install

5. Echoing value of variable when value contains "$"

6. External ISDN connection can't login to ISP

7. Stripping out value from file, value is a variable though.

8. What exactly are the difference of Mandrake security level?

9. settimeofday(2) succeeds for microsecond value more than USEC_PER_SEC and for negative value

10. value of a value question...

11. Find numeric value in xml file, recalculate, and replace old value

12. How to enter values during pkgadd installation and pass those values to postinstall process?