GNU Sed breaks with tab a a delimiter?
I am wondering what is going wrong here. Using a space as a delimiter works but with a tab it seems to fail.
% sed --version
sed (GNU sed) 4.8
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
Paolo Bonzini, Jim Meyering, and Assaf Gordon.
This sed program was built without SELinux support.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
% sed "s ^ foo " <<<hi
foohi
% sed "s\t^\tfoo\t" <<<hi
sed: -e expression #1, char 11: unknown option to `s'
Why doesn't a tab character work as a delimiter. I can confirm that the tab escape sequence is being expanded properly.
% echo sed "s\t^\tfoo\t" | cat -vT
sed s^I^^Ifoo^I
You passed \
as the delimiter, not the tab character.
sed 's\ stuff \ otherstuff \' <<<' stuff '
t
is unknown flag to s
command, hence the error
sed "s\t^\tfoo\t"
^ - command
^^ - regex
^ ^ ^ - delimiter
^^^^ - replacement string
^ - invalid flag to `s` command.
Why doesn't a tab character work as a delimiter
It does - pass the literal tab character, not two characters \
and t
sed $'s\t^\tfoo\t' <<<hi
# or
tab=$(printf '\t')
sed "s/${tab}^${tab}foo%{tab}" <<<hi
I can confirm that the tab escape sequence
You can confirm that when using echo in your shell then echo replaces \t
sequences into a tab. This does not affect sed
in any way. You can pass the result of echo to sed, for example.
# will work in zsh without BSD_ECHO option
sed "$(echo 's\t^\tfoo\t')" <<<hi
# to be portable use printf
sed "$(printf 's\t^\tfoo\t')" <<<hi
See https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html .