Возврат каретки, LineFeed и поведение Sed

У меня нет os-x, чтобы попробовать, но, возможно, эта альтернатива работает:

seq -f '%02.0f' 0 6
1
07.04.2018, 15:07
1 ответ

Из Руководства GNU sed -Как работает sed

sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.

Из спецификации POSIX(спасибо steeldriver за ссылку)

In default operation, sed cyclically shall append a line of input, less its terminating newline, into the pattern space. Normally the pattern space will be empty, unless a D command terminated the last cycle. The sed utility shall then apply in sequence all commands whose addresses select that pattern space, and at the end of the script copy the pattern space to standard output (except when -n is specified) and delete the pattern space. Whenever the pattern space is written to standard output or a named file, sed shall immediately follow it with a newline.


tl;dr разделитель входных записей (, который по умолчанию является новой строкой )удаляется перед выполнением команд, а затем добавляется обратно при печати записи


Однако есть случаи, когда можно манипулировать символом новой строки. Некоторые примеры приведены ниже:

$ # this would still not allow newline of second line to be manipulated
$ seq 5 | sed 'N; s/\n/ : /'
1 : 2
3 : 4
5

$ # here ASCII NUL is input record separator, so newline can be freely changed
$ seq 5 | sed -z 's/\n/ : /g'
1 : 2 : 3 : 4 : 5 :  

$ # default newline separator, so NUL character can be changed
$ printf 'foo\0baz\0xyz\0' | sed 's/\x0/-/g'
foo-baz-xyz-
$ # NUL character is separator, so it cannot be changed now
$ printf 'foo\0baz\0xyz\0' | sed -z 's/\x0/-/g' | cat -A
foo^@baz^@xyz^@
7
27.01.2020, 23:11

Теги

Похожие вопросы