通过使用shell脚本进行匹配来添加单行末尾

时间:2014-04-14 09:48:01

标签: bash shell sed solaris

我有一个shell脚本test.sh,内容是shell脚本,我使用exec命令运行java命令

export T1=
exec java  -XX:+UseConcMarkSweepGC -XX:+PrintClassHistogram 
..
dosomething

现在我已经通过动态修改参数来添加" -Dfile.encoding = UTF-8"在solaris中,为此我使用sed命令

export T1
exec java  -XX:+UseConcMarkSweepGC -XX:+PrintClassHistogram  -Dfile.encoding=UTF-8
...
dosomething

我;是sed commad的新手而且它工作不正常可以有其他替代方案

sed   -e "s/^\(exec \java.*\)$/\1 -Dfile.encoding=UTF-8/" test.sh > $test.sh.tmp

1 个答案:

答案 0 :(得分:1)

您可以使用此sed:

sed -i.bak '/exec java/s/$/-Dfile.encoding=UTF-8/' test.sh

cat test.sh
export T1=
exec java  -XX:+UseConcMarkSweepGC -XX:+PrintClassHistogram -Dfile.encoding=UTF-8
..
dosomething