1

我有一些字符串,例如:

样本输入:

Also known as temple of the city,
xxx as Pune Banglore as kolkata Delhi India,
as Mumbai India or as Bombay India,
Calcutta,India is now know as Kolkata,India,

从上面我想将as xxx xxxx xx转换为as xxx_xxxx_xx,并且应该在最后一个as之后生效。

上面的示例输出:

Also known as temple_of_the_city,
xxx as Pune Banglore as kolkata_Delhi_India,
as Mumbai India or as Bombay_India,
Calcutta,India is now know as Kolkata,India,

最后一行之后不应有空格分隔的字符串。

如果不清楚,请告诉我。谢谢

4

2 回答 2

2

保罗说得对,这并不是一项简单的任务。这是sed我放在一起的解决方案:

sed 's/\(.*as \)/\1\n/;h;y/ /_/;G;s/.*\n\(.*\)\n\(.*\)\n.*/\2\1/' file.txt

演示您的数据:

$ echo 'Also known as temple of the city,
> xxx as Pune Banglore as kolkata Delhi India,
> as Mumbai India or as Bombay India,
> Calcutta,India is now know as Kolkata,India,' | \
> sed 's/\(.*as \)/\1\n/;h;y/ /_/;G;s/.*\n\(.*\)\n\(.*\)\n.*/\2\1/'
Also known as temple_of_the_city,
xxx as Pune Banglore as kolkata_Delhi_India,
as Mumbai India or as Bombay_India,
Calcutta,India is now know as Kolkata,India,
于 2013-03-06T20:11:12.557 回答
1

我倾向于使用瑞士军用电锯 Perl,但 sed 也是一种选择。在任何一种情况下,您都会看到大量的学习曲线。

您描述的替换可能足够复杂,您最好编写一个脚本而不是尝试将其作为一个衬里来完成。

如果您要编写脚本并且还不了解 Perl,那么只要它具有某种文本模式匹配语法,您就没有理由不选择您选择的脚本语言(python、ruby 等)。

我不知道进行这种复杂模式匹配和替换的简单、浅层学习曲线方法。这是一次性的事情,您只需要进行此更换吗?或者你将来会做类似的复杂模式替换。如果你打算经常这样做,你真的应该花时间学习一些脚本语言,但我不会把我的 Perl 偏见强加给你。只需选择任何似乎可以访问的语言。

于 2013-03-06T20:05:26.307 回答