Sed在命令行中不起作用,但是在线测试regex101中的正则表达式有效 [英] Sed doesn't work in command line however regular expression in online test regex101 works
问题描述
我有一个字符串
2017年7月20日,11:03:37.620 fc384c3d-9a75-459d-ba92-99069db0e7bf
我需要删除从行首到UUID子字符串的所有内容(这是一个制表符,\
我的正则表达式如下:
^ \s * July(。*)\t
当我在regex101都能正常运行: https://regex101.com/r/eZ1gT7/1077
但是,当我将其插入sed命令时,它不会做任何替换:
less PensionQuery.txt | sed -e's / ^ \s * July(。*)\t //'
其中PensionQuery.txt是一个充满与上述相似的行的文件。因此,以上命令只是吐出未经修改的文件内容。
我的 sed 命令是否错误?
任何想法吗?
正则表达式正确,您尝试 sed
不是 >使用-regexp扩展
'- E'
'-regexp-extended'
使用扩展正则表达式而不是基本的
正则表达式。扩展的正则表达式是egrep
接受的正则表达式;它们
可以更清晰,因为它们的反斜杠通常较少。
历史上这是GNU
扩展名,但是-E
扩展名有
自添加以来符合POSIX
标准
echo -e $'2017年7月20日,11:03:37.620\tfc384c3d-9a75-459d-ba92-99069db0e7bf'| | sed -E's / ^ \s * July(。*)\t //'
fc384c3d-9a75-459d-ba92-99069db0e7bf
基本和扩展正则表达式表达式是指定模式语法的两种变体。
sed
(和grep
中类似)是默认的正则表达式(BRE)。扩展正则表达式语法(ERE)通过使用-r
或-E
选项(以及类似的< c $ c> grep -E )。
I have a string like
July 20th 2017, 11:03:37.620 fc384c3d-9a75-459d-ba92-99069db0e7bf
I need to remove everything from the beginning of the line till the UUID substring (it's a tab, \t just before the UUID).
My regex looks like that:
^\s*July(.*)\t
When I test it in regex101 it all works beatufully: https://regex101.com/r/eZ1gT7/1077
However, when I plonk that into a sed command it doesn't do any substitution:
less pensionQuery.txt | sed -e 's/^\s*July(.*)\t//'
where pensionQuery.txt is a file full of the lines similar to the above. So the command above simply spits out unmodified file contnent.
Is my sed command wrong?
Any ideas?
The regex is right, you are not trying sed
with --regexp-extended
'-E'
'--regexp-extended'
Use extended regular expressions rather than basic regular expressions. Extended regexps are those that
egrep
accepts; they can be clearer because they usually have fewer backslashes. Historically this was aGNU
extension, but the-E
extension has since been added to thePOSIX
standard
echo -e $'July 20th 2017, 11:03:37.620\tfc384c3d-9a75-459d-ba92-99069db0e7bf' | sed -E 's/^\s*July(.*)\t//'
fc384c3d-9a75-459d-ba92-99069db0e7bf
Also a simple read-up on Basic (BRE) and extended (ERE) regular expression
Basic and extended regular expressions are two variations on the syntax of the specified pattern. Basic Regular Expression (BRE) is the default in
sed
(and similarly ingrep
). Extended Regular Expression syntax (ERE) is activated by using the-r
or-E
options (and similarly,grep -E
).
这篇关于Sed在命令行中不起作用,但是在线测试regex101中的正则表达式有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!