So I have the following file:First LineSecond Line Third Line3 0.3742 0.7771 0.245Fourth Line4 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.245Second Line 0.777Third Line 0.374Fourth Line 0.687...
Reorganizing lines in a text file
text processing;files;awk
With POSIX awk:awk '$1 in a {print a[$1], $0; next}; {a[++l] = $0}' <fileNote that the output isn't sorted, and will be missed if int line appear before referred line.With gawk or bwk-awk, 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] }' <fileWith other awk implementation, you need to write your own function to sort the array index.This also hold the whole file content in memory.