What is the proper way to insert tab in sed?

2020-05-25 06:37发布

What is the proper way to insert tab in sed? I'm inserting a header line into a stream using sed. I could probably do a replacement of some character afterward to put in tab using regular expression, but is there a better way to do it?

For example, let's say I have:

some_command | sed '1itextTABtext'

I would like the first line to look like this (text is separated by a tab character):

text    text

I have tried substituting TAB in the command above with "\t", "\x09", " " (tab itself). I have tried it with and without double quotes and I can't get sed to insert tab in between the text.

I am trying to do this in SLES 9.

标签: linux sed
7条回答
成全新的幸福
2楼-- · 2020-05-25 07:26

To illustrate the fact the BRE syntax for sed does mention that \t is not portable, Git 2.13 (Q2 2017) gets rid of it.

See commit fba275d (01 Apr 2017) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 3c833ca, 17 Apr 2017)

contrib/git-resurrect.sh: do not write \t for HT in sed scripts

Just like we did in 0d1d6e5 ("t/t7003: replace \t with literal tab in sed expression", 2010-08-12, Git 1.7.2.2), avoid writing "\t" for HT in sed scripts, which is not portable.

-   sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p'     
+   sed -ne 's~^\([^ ]*\) .*     checkout: moving from '"$1"' .*~\1~p'
                            ^^^^
                             |
                        (literal tab)
查看更多
登录 后发表回答