【发布时间】:2012-03-29 01:13:00
【问题描述】:
我正在尝试使用命令行 perl 替换多行文件中的文本。我正在使用 Ubuntu Natty。
以下是我的文本文件(称为 test.txt)的内容:
[mysqld]
#
# * Basic Settings
#
#
# * IMPORTANT
# If you make changes to these settings and your system uses apparmor, you may
# also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
#
user = mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
skip-external-locking
下面是我的 perl 命令:
perl -i -pe "s/(\[mysqld\][^\^]+)/\1\nsometext/g" test.txt
但是,我最终得到的不是替换文件中的所有文本,而是:
[mysqld]
sometext#
# * Basic Settings
#
#
# * IMPORTANT
# If you make changes to these settings and your system uses apparmor, you may
# also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
#
user = mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
skip-external-locking
#
我在 RegexBuddy for Perl 中尝试了 Regex,它匹配文本文件中的所有内容,但由于某种原因,它无法在命令行上使用 perl。
我将不胜感激。
提前致谢。
【问题讨论】:
-
我不确定预期的结果应该是什么。
-
@cornuz 我认为从问题中可以明显看出这一点。我说“不是替换文件中的所有文本,[以下]是我最终得到的......”所以这几乎解释了它。同样从正则表达式来看,我认为我需要什么结果似乎很清楚。
标签: linux perl ubuntu command-line