替换复杂字符串与键为任何整数值

huangapple go评论62阅读模式
英文:

perl one liners + replace complex string with key with any integer value

问题

我们正在尝试从文件exampl.txt中完全删除单词-XX:CMSInitiatingOccupancyFraction=50(其中值50可以是任何整数值)。

为此任务,我们使用了以下的Perl一行命令,但没有成功。

perl -pi -e 's/\s*\K\Q-XX:CMSInitiatingOccupancyFraction=(^|\s)\d{1,2}\E\s*//' exampl.txt
perl -pi -e 's/\s*\K\Q-XX:CMSInitiatingOccupancyFraction=\d{1,2}\E\s*//' exampl.txt
perl -pi -e 's/\s*\K\Q-XX:CMSInitiatingOccupancyFraction=[[:digit:]\E\s*//' exampl.txt

文件示例:

SHARED_HADOOP_DATANODE="-server -XX:ParallelGCThreads=8 -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -Xms{{namenode_heapsize}}"

有关我的Perl语法有何问题的建议吗?

英文:

we are trying to remove completely the word - -XX:CMSInitiatingOccupancyFraction=50 from the file exampl.txt ( when value 50 could be any integer value )

for this task we used the following perl one liners , but without success

 perl -pi -e 's/\s*\K\Q-XX:CMSInitiatingOccupancyFraction=(^|\s)d{1,2}\E\s*//' exampl.txt
 perl -pi -e 's/\s*\K\Q-XX:CMSInitiatingOccupancyFraction=d{1,2}\E\s*//' exampl.txt 
 perl -pi -e 's/\s*\K\Q-XX:CMSInitiatingOccupancyFraction=[[:digit:]\E\s*//' exampl.txt 

example of the file:

SHARED_HADOOP_DATANODE="-server -XX:ParallelGCThreads=8 -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -Xms{{namenode_heapsize}}" 

any suggestion what is wrong with my perl syntax?

答案1

得分: 4

perl -i -pe's/-XX:CMSInitiatingOccupancyFraction=\S+\s*//' exampl.txt
英文:
perl -i -pe's/-XX:CMSInitiatingOccupancyFraction=\S+\s*//' exampl.txt

There are a number of problems in your attempts.

  • \Q...\E escapes all non-word characters except those processed during the string-building phase ($, \E, \l, \L, \u, \U and the delimiter). You absolutely don't want this. \Q...\E is almost exclusively used as \Q$text\E.

  • d{1,2} should be \d{1,2}. I switched to \S+ because there's no reason to be more specific.

  • [[:digit:] should be [[:digit:]]+. \d is preferred over [:digit:], giving us [\d]+, which simplifies to \d+.

  • ^ doesn't make sense there. It will never match since it matches the start of the string (without the m modifier) or the start of a line (with m).

  • The leading \s*\K is useless.

huangapple
  • 本文由 发表于 2023年5月30日 02:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定