如何使用一行代码更新 INI 文件中的名称?

发布于 2024-10-31 16:11:22 字数 1785 浏览 6 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短暂陪伴 2024-11-07 16:11:22

使用专门的模块,例如 Config::Tiny解析配置文件。使用它的单行:

perl -MConfig::Tiny -we '$file = shift; $config = Config::Tiny->read($file); $config->{"Pagebar Button Skin"}->{"Margin Top"} = 0; $config->write($file)' test.txt

Use a specialised module like Config::Tiny for parsing configuration files. A one-liner using it:

perl -MConfig::Tiny -we '$file = shift; $config = Config::Tiny->read($file); $config->{"Pagebar Button Skin"}->{"Margin Top"} = 0; $config->write($file)' test.txt
梦明 2024-11-07 16:11:22

不要使用正则表达式来做到这一点。使用模块。 CPAN 有 Config::INIConfig::IniFiles

Don't do it with a regex. Use a module. CPAN has Config::INI and Config::IniFiles.

在巴黎塔顶看东京樱花 2024-11-07 16:11:22

由于 ini 文件通常处于段落模式,您可以尝试:

perl -p00 -e '/Pagebar Button Skin/ && s/(Margin Top.*=)\s*\d/$1 0/' file.ini

输出:

[Pagebar Button Skin]
Type                        = BoxStretch
Tile Center                 = pagebar/top/inactive.png
StretchBorder               = 12
Margin Top                  = 0
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

对结果感到满意后,将 -i.bak 添加到单行代码中

Since ini files are usually in paragraph mode you can try:

perl -p00 -e '/Pagebar Button Skin/ && s/(Margin Top.*=)\s*\d/$1 0/' file.ini

Output:

[Pagebar Button Skin]
Type                        = BoxStretch
Tile Center                 = pagebar/top/inactive.png
StretchBorder               = 12
Margin Top                  = 0
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

Once you are satisfied with the results add -i.bak to one-liner

红尘作伴 2024-11-07 16:11:22

尝试沿线进行一些操作

perl -pe 'if (/^\s*\[Pagebar Button Skin\]/../^\s*\[/) { s/(Margin\s+Top\s*=\s*)1/${1}0/ }'

这将仅在页面栏部分的开头和下一部分之间应用替换。另外,您可以使用一些 INI 模块,将文件作为结构读入 perl,更改您想要的内容并将其写出来。取决于什么更适合您的需求。

Try something along the line

perl -pe 'if (/^\s*\[Pagebar Button Skin\]/../^\s*\[/) { s/(Margin\s+Top\s*=\s*)1/${1}0/ }'

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文