$ perl --version
This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
$ echo -e "foo\nbar" > baz.txt
$ perl -p -e 's/foo\nbar/FOO\nBAR/m' baz.txt
foo
bar
Run Code Online (Sandbox Code Playgroud)
我怎样才能让这个替换工作?
我正在尝试使用命令行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
Run Code Online (Sandbox Code Playgroud)
以下是我的perl命令:
perl -i -pe "s/(\[mysqld\][^\^]+)/\1\nsometext/g" test.txt
Run Code Online (Sandbox Code Playgroud)
但是,下面是我最终得到的内容,而不是替换文件中的所有文本:
[mysqld]
sometext#
# * Basic Settings
#
#
# * IMPORTANT
# If you make changes to …Run Code Online (Sandbox Code Playgroud)