1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| $ cat msg { 'localhost' => ['domain_name' => 'default', 'domain_type' => 'mobile'], }
# 替换文件内容并打印 $ sed '/localhost/p; s/localhost/dev.ops/' msg { 'localhost' => ['domain_name' => 'default', 'domain_type' => 'mobile'], 'dev.ops' => ['domain_name' => 'default', 'domain_type' => 'mobile'], }
# 替换文件内容并将输出重定向到新文件 $ sed '/localhost/p; s/localhost/dev.ops/' msg > new_msg $ cat new_msg { 'localhost' => ['domain_name' => 'default', 'domain_type' => 'mobile'], 'dev.ops' => ['domain_name' => 'default', 'domain_type' => 'mobile'], } # 源文件并未更改 $ cat msg { 'localhost' => ['domain_name' => 'default', 'domain_type' => 'mobile'], }
# 在源文件修改并保存源文件至备份文件 $ sed -i 'msg_backup' '/localhost/p; s/localhost/dev.ops/' msg $ ls msg msgmsg_backup new_msg # 打印源文件备份 $ cat msgmsg_backup { 'localhost' => ['domain_name' => 'default', 'domain_type' => 'mobile'], } # 打印源文件 $ cat msg { 'localhost' => ['domain_name' => 'default', 'domain_type' => 'mobile'], 'dev.ops' => ['domain_name' => 'default', 'domain_type' => 'mobile'], }
# 直接修改源文件并且不保存备份 $ sed -i '/localhost/p; s/localhost/dev.worker/' msgmsg_backup # $ sed -i '' '/localhost/p; s/localhost/dev.worker/' msgmsg_backup $ cat msgmsg_backup { 'localhost' => ['domain_name' => 'default', 'domain_type' => 'mobile'], 'dev.worker' => ['domain_name' => 'default', 'domain_type' => 'mobile'], }
$ ls msg msgmsg_backup new_msg
# # $ cat script/config.sh workers=6666 threads_compute=10 threads=101 $ sed -i "s/workers=[0-9]*/workers=9/" ./script/config.sh $ cat script/config.sh workers=9 threads_compute=10 threads=101
# group sed -i "s/workers=([0-9]+)/\1/" /path/to/file
|