使用SED替换模式会导致出现额外的字符

时间:2022-11-16 16:52:41

I have a file and want to replace a pattern with another string.

我有一个文件,想用另一个字符串替换一个模式。

#PBS -N bench1-M1-plt2-size15
#PBS -o /home/results/bench1-M1-plt2-size15.out
./run -d ./results/ --config=bench1-M1-plt2-size15.conf \ 
--results=bench1-M1-plt2-size15.res config/myConfig.txt -F 2000000 

I use this command

我使用这个命令

sed 's/M1-[^-]*-[^-]/M1-plt32-size10/g' filename

However the output file looks like this:

但是输出文件是这样的:

#PBS -N M1-plt32-size100
#PBS -o /home/mahmood/gem5/results/M1-plt32-size100.out
./run -d ./results/ --config=bench1-M1-plt32-size100.conf \
--results=bench1-M1-plt32-size100.res config/myConfig.txt -F 2000000 

Please note an extra '0' character after "size10". As you can see, in the SED command, size is set to 10, however the output file is "size100"

请在“size10”后注意一个额外的“0”字符。可以看到,在SED命令中,大小被设置为10,但是输出文件是“size100”

What is the problem and how can I fix that?

问题是什么,我该如何解决?

1 个解决方案

#1


3  

Your regex is not correct. You have [^-] which will match only 1 non-hyphen character. I believe this is what you intended:

您的regex不正确。你有[^ -]将匹配只有1 non-hyphen字符。我相信这就是你想要的:

sed 's/M1-[^-]*-[^\.]*/M1-plt32-size10/' filename

OUTPUT:

输出:

#PBS -N bench1-M1-plt32-size10
#PBS -o /home/results/bench1-M1-plt32-size10.out
./run -d ./results/ --config=bench1-M1-plt32-size10.conf \ 
--results=bench1-M1-plt32-size10.res config/myConfig.txt -F 2000000

#1


3  

Your regex is not correct. You have [^-] which will match only 1 non-hyphen character. I believe this is what you intended:

您的regex不正确。你有[^ -]将匹配只有1 non-hyphen字符。我相信这就是你想要的:

sed 's/M1-[^-]*-[^\.]*/M1-plt32-size10/' filename

OUTPUT:

输出:

#PBS -N bench1-M1-plt32-size10
#PBS -o /home/results/bench1-M1-plt32-size10.out
./run -d ./results/ --config=bench1-M1-plt32-size10.conf \ 
--results=bench1-M1-plt32-size10.res config/myConfig.txt -F 2000000