You can do all that in sed or awk.
Are all the lines labeled rtg1 or rtg2 and nothing else? Are the rtg1 and rtg2 lines always in that order or can they be in any order?
I'll make the following assumptions:
1) this is a large file with repeated rtg1 and rtg2 lines.
2) the file only contains lines starting with rtg1 or rtg2.
3) the lines always appear in the order rtg1, rtg2. Although not necessarily consecutively.
4) '^m' means end of line
5) the key following rtg1 is unique for each set of lines and is always 13 characters long.
create a file called script.awk containing:
Code:
/rtg1/ { part1 = substr($0, 1,23);
part2 = substr($0, 24);
gsub(/ /, "", part2);
line = sprintf("%s%s", part1, part2);
key = substr($0, 5,13);
save[key] = line;
}
/rtg2/ { key = substr($0, 5,13);
if (length(save[key]) == 0)
printf("Error: no rtg1 line found for %s\n", $0);
else
printf("%s%s\n", save[key], substr($0, 18));
}
then run the following from the command line:
# awk -f script.awk file-you-want-to-process > result-file
.