我的参数文件中包含以下内容。
[s_m_imgpstna]
$DBConnection1=Insight_QAT331_DB
[s_m_drcgctln]
$DBConnection1=Insight_QAT291_DB
[s_m_ioxgciln]
$DBConnection1=Insight_QAT291_DB
[s_m_reddecna]
$DBConnection1=Insight_QAT291_DB
[s_m_cd2gcpna]
$DBConnection1=Insight_QAT291_DB
[s_m_cd2gcpna]
$DBConnection1=Insight_QAT291_DB
[s_m_cd2gctna]
$DBConnection1=Insight_QAT291_DB
[s_m_g92gctsp]
$DBConnection1=Insight_QAT291_DB
[s_m_os2gcceu]
$DBConnection1=Insight_QAT291_DB
[s_m_ccpgctdf]
$DBConnection1=Insight_QAT291_DB
[s_m_ew4gciln]
$DBConnection1=Insight_QAT307_DB
[s_m_dr6gctln]
$DBConnection1=Insight_QAT291_DB
[s_m_dr2gctln]
$DBConnection1=Insight_QAT291_DB
[s_m_dr4gctln]
$DBConnection1=Insight_QAT291_DB
现在我想搜索模式“cd2gcpna”,它在文件中有两次出现
[s_m_cd2gcpna]
$DBConnection1=Insight_QAT291_DB
[s_m_cd2gctna]
$DBConnection1=Insight_QAT291_DB
现在我想替换所有匹配项下面的字符串([s_m_cd2gctna]
)。
$DBConnection1=Insight_**QAT291**_DB
替换为
$DBConnection1=Insight_**QAT308**_DB
答案 0 :(得分:0)
awk 解决方案:
<router-outlet></router-outlet>
如果你想用GNU awk改变你的文件,你可以这样做:
$ awk '/cd2gcpna/{p=1;print;next} \
p{sub(/291/,"308");p=0}1' file
并使用params:
awk -i inplace '/cd2gcpna/{p=1;print;next} \
p{sub(/291/,"308");p=0}1' file
从shell脚本:
$ awk -v pat="cd2gcpna" -v val1=291 -v val2=308 '$0~pat{p=1;print;next} \
p{sub(val1,val2);p=0}1' file
并将其命名为:
$ cat test.sh
#!/bin/bash
pat=$1
val1=$2
val2=$3
file=$4
awk -v pat="$1" -v val1=$2 -v val2=$3 '$0~pat{p=1;print;next} \
p{sub(val1,val2);p=0}1' $file
答案 1 :(得分:0)
sed
方法:
sed -i '/cd2gcpna/{n;s/QAT291/QAT308/}' file
要将它包装在一个函数中,(比脚本更容易测试,代码的方式几乎相同):
# usage: rep_after flagstring fromstring tostring file
rep_after() { sed -i '/'"$1"'/{n;s/'"$2"'/'"$3"'/}' "$4" ; }
并称之为:
rep_after cd2gcpna QAT291 QAT308 file