如何使用一行代码更新 INI 文件中的名称?
使用 Perl 单行正则表达式命令替换 INI 文件特定部分中的值?
我想将“Margin Top”值 1 替换为 0。该部分必须是“[Pagebar Button Skin]”。
在我尝试使用 RegExr 中的 global 和 dotall 的“(\[Pagebar Button Skin\].+?Margin Top\s+?=\s+?)(1)”后,我能够使用“$10”或“1 0 美元”。
不幸的是,当我运行命令时它不起作用:
perl.exe -i.bak -pe "s/(\[Pagebar Button Skin\].+?Margin Top\s+?=\s+?)(1)/$1 0/g" test.txt
这是我的 test.txt 文件中的“[Pagebar Button Skin]”部分:
[Pagebar Button Skin]
Type = BoxStretch
Tile Center = pagebar/top/inactive.png
StretchBorder = 12
Margin Top = 1
Margin Right = -5
Margin Left = -5
Margin Bottom = 0
Padding Left = 12
Padding Top = 5
Padding Right = 9
Padding Bottom = 6
Spacing = 3
Text Color = #111111
编辑:
我必须创建一个 Perl 脚本才能让正则表达式工作。也许 Perl 不喜欢我的 Windows 环境。
命令:
perl.exe skin.pl skin.ini
skin.pl:
$/ = undef; # Setting $/ to undef causes <FILE> to read the whole file into a single string.
# Store filename from argument
my $filename = shift;
# Open the file as read only and then store the file text into a string.
open(FILE, "<", $filename) || die "Could not open $filename\n";
my $text = <FILE>;
close(FILE);
# Re-open the file as writable and then overwrite it with the replaced text.
open(FILE, "+>", $filename) || die "Could not open $filename\n";
$text =~ s/(\[Pagebar\s+?Button\s+?Skin\].+?Margin\s+?Top\s+?=\s+?)(1)/${1}0/sg;
#print $text; # Print the text to screen
print {FILE} $text; # Print the text to the file
close(FILE);
Replace value in a specific section of an INI file using Perl one-liner regex command?
I want to replace the "Margin Top" value of 1 to 0. The section must be "[Pagebar Button Skin]".
After I tried "(\[Pagebar Button Skin\].+?Margin Top\s+?=\s+?)(1)" with global and dotall in RegExr, I was able to replace the value with 0 using "$10" or "$1 0".
Unfortunately, it doesn't work when I run my command:
perl.exe -i.bak -pe "s/(\[Pagebar Button Skin\].+?Margin Top\s+?=\s+?)(1)/$1 0/g" test.txt
Here is the "[Pagebar Button Skin]" section in my test.txt file:
[Pagebar Button Skin]
Type = BoxStretch
Tile Center = pagebar/top/inactive.png
StretchBorder = 12
Margin Top = 1
Margin Right = -5
Margin Left = -5
Margin Bottom = 0
Padding Left = 12
Padding Top = 5
Padding Right = 9
Padding Bottom = 6
Spacing = 3
Text Color = #111111
EDIT:
I had to create a Perl script in order to get the regex to work. Maybe Perl doesn't like my Windows environment.
Command:
perl.exe skin.pl skin.ini
skin.pl:
$/ = undef; # Setting $/ to undef causes <FILE> to read the whole file into a single string.
# Store filename from argument
my $filename = shift;
# Open the file as read only and then store the file text into a string.
open(FILE, "<", $filename) || die "Could not open $filename\n";
my $text = <FILE>;
close(FILE);
# Re-open the file as writable and then overwrite it with the replaced text.
open(FILE, "+>", $filename) || die "Could not open $filename\n";
$text =~ s/(\[Pagebar\s+?Button\s+?Skin\].+?Margin\s+?Top\s+?=\s+?)(1)/${1}0/sg;
#print $text; # Print the text to screen
print {FILE} $text; # Print the text to the file
close(FILE);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用专门的模块,例如
Config::Tiny
解析配置文件。使用它的单行:Use a specialised module like
Config::Tiny
for parsing configuration files. A one-liner using it:不要使用正则表达式来做到这一点。使用模块。 CPAN 有 Config::INI 和 Config::IniFiles。
Don't do it with a regex. Use a module. CPAN has Config::INI and Config::IniFiles.
由于 ini 文件通常处于段落模式,您可以尝试:
输出:
对结果感到满意后,将 -i.bak 添加到单行代码中
Since ini files are usually in paragraph mode you can try:
Output:
Once you are satisfied with the results add -i.bak to one-liner
尝试沿线进行一些操作
这将仅在页面栏部分的开头和下一部分之间应用替换。另外,您可以使用一些 INI 模块,将文件作为结构读入 perl,更改您想要的内容并将其写出来。取决于什么更适合您的需求。
Try something along the line
This will apply the substitution only between the start of the Pagebar section, and the next section. Also, you can use some INI module, read the file into perl as structure, change what you want and write it out. Depends on what better suits your needs.