Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeat First Value in Field y for Values of x that are the Same

Status
Not open for further replies.

samincal

Technical User
Aug 14, 2007
2
US
For every set of i in $1, I'd like to have the first element of $2 appear in the remaining elements of $2. This process would repeat for each i in $1.

I.e.:
The data looks roughly This is what I'd like to have it look like:
like this:

2 30 2 30
2 0 2 30
2 0 2 30
2 0 2 30
2 0 2 30
2 0 2 30
3 23 3 23
3 0 3 23
3 0 3 23
3 0 3 23
3 0 3 23
5 28 5 28
5 0 5 28
5 0 5 28
5 0 5 28
6 20 6 20
6 0 6 20
6 0 6 20
. .
. .
. .
 
Sorry for not being more clear. The input data is on the left, the output data on the right. This is a case of multiple records per individual (given by field $1). For simplicity, I've just shown a small (most relevant) portion of the data. I'd like to repeat the first value of field $2 in the remaining field $2 records within the individual (for the first individual shown, that would be lines 2-5). Thus, the values in $2 will all be the same for a given individual. This repeats itself for each individual in the data set.

Thanks for showing an interest. Any ideas much appreciated.

Samincal
 
Hi

Samincal said:
The input data is on the left, the output data on the right.
Oops. Would never guess that... [banghead]

So if [tt]$2==0[/tt] then print the last non-zero value of [tt]$2[/tt] instead ?
Code:
awk '!$2{$2=l}$2{l=$2}1' /input/file > /output/file

Feherke.
 
Why not simply this ?
Code:
awk '$2{x=$2}{print $1,x}' /path/to/input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top