sed命令和分组

时间:2013-08-14 21:40:29

标签: regex sed

我有一个名为test的文件,它包含:

EMS_INSTANCES_DIR=/WEB_DATA/home/edofc24/EMS-INSTANCES/async
EMS_SHARED_CONFIG=/WEB_DATA/home/edofc24/EMS-INSTANCES/async/shared/config
EMS_SHARED_DATA=/WEB_DATA/home/edofc24/EMS-INSTANCES/async/shared/data
EMS_JMS_PORT=15244
EMS_INTERFACE=tcp://adecpcas:15244
EMS_KEYS=/WEB_DATA/home/edofc24/EMS-INSTANCES/keys

我想在=之后更改路径到/ test / path

我使用sed来做到这一点:

sed 's/\(^EMS_[SIJ].*\=\)\(\/.*[a-z]$\)/\1\/test/path/' test

但没有发生变化,为什么?。

1 个答案:

答案 0 :(得分:1)

sed 's:=/.*:=/test/path:' file

示例:

kent$  echo "EMS_INSTANCES_DIR=/WEB_DATA/home/edofc24/EMS-INSTANCES/async
EMS_SHARED_CONFIG=/WEB_DATA/home/edofc24/EMS-INSTANCES/async/shared/config
EMS_SHARED_DATA=/WEB_DATA/home/edofc24/EMS-INSTANCES/async/shared/data
EMS_JMS_PORT=15244
EMS_INTERFACE=tcp://adecpcas:15244"|sed 's:=/.*:=/test/path:'
EMS_INSTANCES_DIR=/test/path
EMS_SHARED_CONFIG=/test/path
EMS_SHARED_DATA=/test/path
EMS_JMS_PORT=15244
EMS_INTERFACE=tcp://adecpcas:15244

如果您想要进行更改,请添加-i

编辑评论:

sed 's@\(^EMS_[SIJ].*=/\).*@\1test/path/@' file

用相同的例子:

kent$  echo "EMS_INSTANCES_DIR=/WEB_DATA/home/edofc24/EMS-INSTANCES/async
EMS_SHARED_CONFIG=/WEB_DATA/home/edofc24/EMS-INSTANCES/async/shared/config
EMS_SHARED_DATA=/WEB_DATA/home/edofc24/EMS-INSTANCES/async/shared/data
EMS_JMS_PORT=15244
EMS_INTERFACE=tcp://adecpcas:15244"|sed 's@\(^EMS_[SIJ].*=/\).*@\1test/path/@'
EMS_INSTANCES_DIR=/test/path/
EMS_SHARED_CONFIG=/test/path/
EMS_SHARED_DATA=/test/path/
EMS_JMS_PORT=15244
EMS_INTERFACE=tcp://adecpcas:15244