Question: So I have the following file:

```
First Line
Second Line 
Third Line
3 0.374
2 0.777
1 0.245
Fourth Line
4 0.687
...

```

And I want to reorganize it so the lines beginning with an int are appended to the line that int refers to:

```
First Line 0.245
Second Line 0.777
Third Line 0.374
Fourth Line 0.687
...

```

Answer: 
With POSIX `awk`:

```
awk '$1 in a {print a[$1], $0; next}; {a[++l] = $0}' <file

```

Note that the output isn't sorted, and will be missed if int line appear before referred line.

With `gawk` or [`bwk-awk`](http://www.cs.princeton.edu/~bwk/btl.mirror/index.html), you print the output sorted easily:

```
gawk '
  $1 in a { a[$1] = a[$1] " " $0; next }
  { a[++l] = $0 }
  END { for (i = 1; i <= length(a); i++) print a[i] }
' <file

```

With other `awk` implementation, you need to write your own function to sort the array index.

This also hold the whole file content in memory.
The solution below gives you a sorted output without the need of using any sorting facility:

```
$1 ~ "[0-9]" {
    line[$1] = $2;
    next;
}
{
    aux[++k] = $0;
}
END {
    for(i = 1; i <= k; i++)
        print aux[i], line[i];
}

```

Output:

```
First Line 0.245
Second Line  0.777
Third Line 0.374
Fourth Line 0.687

```

If you want a well formatted print, just use `printf`.