gpt4 book ai didi

regex - Sed 在命令行中不起作用,但是在线测试 regex101 中的正则表达式有效

转载 作者:行者123 更新时间:2023-12-01 12:19:02 26 4
gpt4 key购买 nike

我有一个像

 July 20th 2017, 11:03:37.620   fc384c3d-9a75-459d-ba92-99069db0e7bf

我需要删除从行首到 UUID 子字符串的所有内容(它是一个制表符,\t 就在 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-extended

'-E''--regexp-extended'


Use extended regular expressions rather than basic regularexpressions. Extended regexps are those that egrep accepts; theycan be clearer because they usually have fewer backslashes.Historically this was a GNU extension, but the -E extension hassince been added to the POSIX 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
还有一个简单的阅读 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 in grep). Extended Regular Expression syntax (ERE) is activated by using the -r or -E options (and similarly, grep -E).

关于regex - Sed 在命令行中不起作用,但是在线测试 regex101 中的正则表达式有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45879435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com