为什么 sed 不替换重叠模式 [英] Why does sed not replace overlapping patterns

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

问题描述

我有一个数据库卸载文件,其中的字段以 <TAB> 字符分隔.我正在通过 sed 运行此文件,以将任何出现的 <TAB><TAB> 替换为 <TAB>\N<TAB>.这样当文件被加载到 MySQL 中时,\N 被解释为 NULL.

I have a database unload file with field separated with the <TAB> character. I am running this file through sed to replace any occurences of <TAB><TAB> with <TAB>\N<TAB>. This is so that when the file is loaded into MySQL the \N in interpreted as NULL.

sed 命令 's/\t\t/\t\N\t/g;'几乎可以工作,除了它只替换第一个实例,例如"......" 变为 "...\N...".

The sed command 's/\t\t/\t\N\t/g;' almost works except that it only replaces the first instance e.g. "...<TAB><TAB><TAB>..." becomes "...<TAB>\N<TAB><TAB>...".

如果我使用 's/\t\t/\t\N\t/g;s/\t\t/\t\N\t/g;'它取代了更多的实例.

If I use 's/\t\t/\t\N\t/g;s/\t\t/\t\N\t/g;' it replaces more instances.

我有一个概念,尽管有/g 修饰符,但这与一场比赛的结束是另一场比赛的开始有关.

I have a notion that despite the /g modifier this is something to do with the end of one match being the start of another.

任何人都可以解释发生了什么,并建议一个可以工作或我需要循环的 sed 命令.

Could anyone explain what is happening and suggest a sed command that would work or do I need to loop.

我知道我可能会切换到 awk、perl、python,但我想知道 sed 中发生了什么.

I know I could probably switch to awk, perl, python but I want to know what is happening in sed.

推荐答案

我知道你想要 sed,但是 sed 根本不喜欢这个,好像专门(见 这里) 不会做你想做的事.但是,perl 会这样做(AFAIK):

I know you want sed, but sed doesn't like this at all, it seems that it specifically (see here) won't do what you want. However, perl will do it (AFAIK):

perl -pe 'while (s#\t\t#\t\n\t#) {}' <filename>

这篇关于为什么 sed 不替换重叠模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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