sed为什么将替换的文本放在行首? [英] Why is sed putting substituted text at the start of a line?

查看:88
本文介绍了sed为什么将替换的文本放在行首?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆