简体   繁体   中英

Sed overwrites match groups from line beginning in OSX

In OSX, sed behaves as below:

echo -en 'xxx abc\r\nxxx aa bb cc\r\n' | sed -E 's/xxx (.*)/\1->/g'
->c
-> bb cc

While I was expecting the following:

echo -en 'xxx abc\r\nxxx aa bb cc\r\n' | sed -E 's/xxx (.*)/\1->/g'
abc->
aa bb cc->

I am simply matching a line using xxx then putting everything else somewhere in a string that I constructed. It seems it is related to \\r , but am not quite sure how to fix it, without first trimming down \\r from the text file first. (The example above does not mention any text file, but you can imagine the output of echo was in a text file).

Any explanation why -> overwrites the group match?

It's caused by the carriage-return ( \\r ) chars.

Tested on OS X:

[STEP 107] $ printf 'xxx abc\r\nxxx aa bb cc\r\n' | sed -E 's/xxx (.*)/\1->/g'
->c
-> bb cc
[STEP 108] $ printf 'xxx abc\r\nxxx aa bb cc\r\n' | sed -E 's/xxx (.*)/\1->/g' \
             | hexdump -C
00000000  61 62 63 0d 2d 3e 0a 61  61 20 62 62 20 63 63 0d  |abc.->.aa bb cc.|
00000010  2d 3e 0a                                          |->.|
[STEP 109] $ printf 'xxx abc\nxxx aa bb cc\n' | sed -E 's/xxx (.*)/\1->/g'
abc->
aa bb cc->
[STEP 110] $ printf 'abc\r->\n'
->c
[STEP 111] $

Take printf 'abc\\r->\\n' for example: It would first print abc , then \\r would move the cursor back to the beginning of the line, then print -> which would overwrite ab , so finally we see ->c .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM