英文:
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 them
modifier) or the start of a line (withm
). -
The leading
\s*\K
is useless.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论