Linux 在 sed 中插入选项卡的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8394066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the proper way to insert tab in sed?
提问by dabest1
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?
在 sed 中插入选项卡的正确方法是什么?我正在使用 sed 将标题行插入到流中。之后我可能可以替换一些字符以使用正则表达式放入制表符,但是有没有更好的方法来做到这一点?
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.
我曾尝试将上面命令中的 TAB 替换为 "\t"、"\x09"、" "(标签本身)。我已经尝试过使用和不使用双引号,但我无法使用 sed 在文本之间插入制表符。
I am trying to do this in SLES 9.
我试图在 SLES 9 中做到这一点。
采纳答案by Jonathan Leffler
You can simply use the sed
i
command correctly:
您可以简单地sed
i
正确使用该命令:
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:
我希望很明显,在“text”和“text2”之间有一个选项卡。在 MacOS X (10.7.2) 上,因此可能在其他基于 BSD 的平台上,我能够使用:
some_command | sed '1i\
text\ttext2'
and sed
translated the \t
into a tab.
并将其sed
翻译\t
成标签。
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.
如果在命令行中sed
无法解释\t
和插入选项卡是一个问题,请使用编辑器创建一个 shell 脚本并运行该脚本。
回答by dabest1
I found an alternate way to insert a tab by using substitution.
我找到了一种使用替换插入选项卡的替代方法。
some_command | sed '1s/^/text\ttext\n/'
some_command | sed '1s/^/text\ttext\n/'
I still do not know of a way to do it using the insert method.
我仍然不知道使用插入方法的方法。
回答by ghoti
Sed can do this, but it's awkward:
Sed 可以做到这一点,但它很尴尬:
% printf "1\t2\n3\t4\n" | sed '1i\
foo bar\
'
foo bar
1 2
3 4
$
(The double backslashes are because I'm using tcsh as my shell; if you use bash, use single backslashes)
(双反斜杠是因为我使用 tcsh 作为我的 shell;如果您使用 bash,请使用单反斜杠)
The space between foo and bar is a tab, which I typed by prepending it with CtrlV. You'll also need to prepend the newlines inside your single quotes with a CtrlV.
foo 和 bar 之间的空格是一个制表符,我通过在它前面加上CtrlV. 您还需要在单引号内添加换行符CtrlV。
It would probably be simpler/clearer to do this with awk:
使用 awk 执行此操作可能更简单/更清晰:
$ printf "1\t2\n3\t4\n" | awk 'BEGIN{printf("foo\tbar\n");} {print;}'
回答by Aaron McDaid
Assuming bash (and maybe other shells will work too):
假设 bash (也许其他 shell 也可以工作):
some_command | sed $'1itext\ttext'
Bash will process escapes, such as \t
, inside $' '
before passing it as an arg to sed.
Bash 将处理转义,例如\t
, inside ,$' '
然后将其作为 arg 传递给 sed。
回答by user 1155692
As most answers say, probably literal tab
char is the best.
正如大多数答案所说,文字tab
字符可能是最好的。
info sed
saying "\t is not portable." :
info sed
说“ \t 不可移植。”:
...
'\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.
...
...
'\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.
...
回答by VonC
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.
为了说明BRE 语法sed
确实提到\t
不可移植的事实,Git 2.13(2017 年第二季度)去掉了它。
See commit fba275d(01 Apr 2017) by Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
--in commit 3c833ca, 17 Apr 2017)
请参阅Junio C Hamano() 的commit fba275d(2017 年 4 月 1 日)。(由Junio C Hamano合并-- --在commit 3c833ca,2017 年 4 月 17 日)gitster
gitster
contrib/git-resurrect.sh
: do not write\t
for HT insed
scriptsJust like we did in 0d1d6e5("
t/t7003
: replace\t
with literal tab insed
expression", 2010-08-12, Git 1.7.2.2), avoid writing "\t
" for HT insed
scripts, which is not portable.
contrib/git-resurrect.sh
: 不要\t
在sed
脚本中为 HT编写就像我们在0d1d6e5 中所做的一样(“
t/t7003
:\t
在sed
表达式中替换为文字选项卡”,2010-08-12,Git 1.7.2.2),避免\t
在sed
脚本中为 HT编写“ ” ,这是不可移植的。
- sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '""' .*~~p'
+ sed -ne 's~^\([^ ]*\) .* checkout: moving from '""' .*~~p'
^^^^
|
(literal tab)
回答by Rajesh Goel
escape the tab character:
转义制表符:
sed -i '/<setup>/ a \\tmy newly added line' <file_name>
sed -i '/<setup>/ a \\tmy newly added line' <file_name>
NOTE: above we have two backslashes (\) first one is for escaping () and the next one is actual tab char (\t)
注意:上面我们有两个反斜杠 (\),第一个用于转义 (),下一个是实际的制表符 (\t)