Laurenhsr
Laurenhsr

Reputation: 3

Why is my sed output to file different from terminal output?

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
111 111
222 222
333 333
444 444
555 555
666 666
SCALE=0.123
ID=MS17_female_8_central_2.jpg

I wanted to move the first coordinate down to be the sixth coordinate, like so;

LM=6
222 222
333 333
444 444
555 555
666 666
111 111
SCALE=0.123
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
222 222
333 333
444 444
555 555
666 666
111 111
SCALE=0.123
ID=MS17_female_16_central2.jpg

LM=6
222 222
333 333
444 444
555 555
666 666
SCALE=0.123
111 111
SCALE=0.124
ID=MS17_female_18_central2.jpg

LM=6
222 222
333 333
444 444
555 555
666 666
SCALE=0.124
111 111
SCALE=0.125
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!

Upvotes: 0

Views: 224

Answers (3)

potong
potong

Reputation: 58578

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).


Applying a bit more thought:

sed '/^LM/{n;h;d};/^SCALE/{H;g}' file

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 142080

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.

Upvotes: 0

sseLtaH
sseLtaH

Reputation: 11247

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

Upvotes: 1

Related Questions