sed为什么将替换的文本放在行首? [英] Why is sed putting substituted text at the start of a line?
问题描述
在Mac OS X(bash)上,我在命令行上使用以下sed:
On Mac OS X (bash), I'm using the following sed at the command line:
$ sed 's/\(\S*\)/\1 ~ /' file1.txt > file2.txt
file1.txt
具有以下行:
some/path_to/somewhere/- some{"stuff here"}-and_etc
some/path_to/somewhere.else- some.more{"stuff here"}-and_etc
因此,我希望〜会出现在第一组非空格字符之后空格开始的地方.相反,我在file2.txt
中得到了这个
So I expect that a ~ will appear where spaces begin after the first set of non-space characters. Instead, I get this in file2.txt
:
~ some/path_to/somewhere/- some{"stuff here"}-and_etc
~ some/path_to/somewhere.else- some.more{"stuff here"}-and_etc
我尝试在\(\S
之前添加^
,但这没什么区别.
I've tried adding ^
ahead of the \(\S
but that makes no difference.
如何使file2.txt
看起来像这样:
some/path_to/somewhere/-~ some{"stuff here"}-and_etc
some/path_to/somewhere.else-~ some.more{"stuff here"}-and_etc
推荐答案
很确定您的sed不理解\S
表示非空白".最有可能尝试匹配零个或多个S
字符",并在字符串的开头找到匹配项.
Pretty sure your sed does not understand \S
to mean "non-whitespace". It's most likely trying to match "zero or more S
characters", and it finds that match at the beginning of the string.
您要坚持使用POSIX字符类:
You want to stick to the POSIX character classes:
sed 's/[^[:blank:]]\+/& ~ /' file1.txt
这篇关于sed为什么将替换的文本放在行首?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!