Newbie here, so apologies for any missing info/silly mistakes!
I have a text file that contains a series of datasets. Each dataset comprises 6 sets of coordinates ('landmarks'), a scale and a specimen ID, like so;
LM=6
1282.36861042892 1113.66360157633
1187.62419327203 1128.4674167571
1140.25198469359 1081.09520817865
1163.93808898281 983.390027985608
1255.72174310355 971.546975840996
1300.13318864584 1021.87994745559
SCALE=0.120625275573192
ID=MS17_female_8_central_2.jpg
I wanted to move the first coordinate down to be the sixth coordinate, like so;
LM=6
1187.62419327203 1128.4674167571
1140.25198469359 1081.09520817865
1163.93808898281 983.390027985608
1255.72174310355 971.546975840996
1300.13318864584 1021.87994745559
1282.36861042892 1113.66360157633
SCALE=0.120625275573192
ID=MS17_female_8_central_2.jpg
So I tried using sed (probably clumsily). This works in the terminal;
sed '/LM/{n;H;d;}; /SCALE/{x;1!G;s/\n//;}' < in.txt
Everything looks good. However, as soon as I try this;
sed '/LM/{n;H;d;}; /SCALE/{x;1!G;s/\n//;}' < in.txt > out.txt
The output in the created file is different. The first chunk of data looks fine, but from then onward, I have also managed to insert the 'SCALE' line from the previous chunk...
LM=6
1184.66343023588 1276.50556856474
1160.97732594666 1356.44617054086
1021.82146324747 1391.9753269747
956.68467645211 1282.42709463704
983.331543777486 1202.48649266092
1119.52664344052 1166.95733622708
SCALE=0.0888817820012996
ID=MS17_female_16_central2.jpg
LM=6
921.155520018276 903.449426009481
870.822548403678 832.391113141813
915.23399394597 705.078302587241
1042.54680450054 699.156776514935
1098.80130218745 770.215089382603
SCALE=0.0888817820012996
1054.38985664515 900.488662973328
SCALE=0.0912839923256591
ID=MS17_female_18_central2.jpg
LM=6
1300.13318864584 1140.31046890171
1311.97624079045 1057.40910388943
1436.32828830887 1004.11536923868
1525.15117939345 1092.93826032326
1510.34736421269 1175.83962533554
SCALE=0.0912839923256591
1383.03455365812 1229.13335998629
SCALE=0.0912839923256591
ID=MS17_female_19_central2.jpg
I can well believe that I did something stupid in my sed, but I cannot work out why the terminal and file outputs would be different.. any help much appreciated!
CodePudding user response:
This might work for you (GNU sed):
sed '/^LM/{n;h;d};/^SCALE/{x;p;x}' file
Match a line beginning LM then print it, fetch the next into the hold space and delete the fetched line.
Match a line beginnng SCALE, swap to the hold space, print it and swap back.
N.B. The h command overwrites what is already in the hold space whereas the H command appends to it (with a newline as a separator). Likewise the g command replaces the current line with the contents of the hold space and the G command appends the hold space to the current line (with a newline as a separator).
CodePudding user response:
Using sed
$ sed '/^LM/{n;x;d};/^SCALE/{x;G}' < input_file > output_file
$ cat output_file
LM=6
1187.62419327203 1128.4674167571
1140.25198469359 1081.09520817865
1163.93808898281 983.390027985608
1255.72174310355 971.546975840996
1300.13318864584 1021.87994745559
1282.36861042892 1113.66360157633
SCALE=0.120625275573192
ID=MS17_female_8_central_2.jpg
CodePudding user response:
why the terminal and file outputs would be different..
You are viewing the output in different programs. The terminal interprets carriage return character as return cursor to the beginning of the line, which causes the line to be overwritten by the next line. While the other programs interprets carriage return character as a newline.
Your sed script H; appends to hold space, so it's only getting bigger.. You might want to h replace hold space.
