What is the proper way to insert tab in sed?

As most answers say, probably literal tab char is the best.

info sed saying "\t is not portable." :

...
'\CHAR'
    Matches CHAR, where CHAR is one of '$', '*', '.', '[', '\', or '^'.
    Note that the only C-like backslash sequences that you can
    portably assume to be interpreted are '\n' and '\\'; in particular
    '\t' is not portable, and matches a 't' under most implementations
    of 'sed', rather than a tab character.
...

You can simply use the sed i command correctly:

some_command | sed '1i\
text    text2'

where, as I hope it is obvious, there is a tab between 'text' and 'text2'. On MacOS X (10.7.2), and therefore probably on other BSD-based platforms, I was able to use:

some_command | sed '1i\
text\ttext2'

and sed translated the \t into a tab.

If sed won't interpret \t and inserting tabs at the command line is a problem, create a shell script with an editor and run that script.


Assuming bash (and maybe other shells will work too):

some_command | sed $'1itext\ttext'

Bash will process escapes, such as \t, inside $' ' before passing it as an arg to sed.

Tags:

Linux

Sed