linuxbashshellperlsed

sed + match word in lines and replaced value with some conditions


we want to prepare sed approach in order to change the value of the key - ParallelGCThreads , and this will happened only on matched word in the line as HADOOP_DATANODE_OPTS

more Hadoop-specific-environment.txt
.
.

export HADOOP_DATA_NODE_OPTS="-server -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:ErrorFile=/var/log/hadoop/$USER/hs_err_pid%p.log 
.
.

so we prepared the following sed syntax

val-32
HADOOP_OPTS=HADOOP_DATA_NODE_OPTS

sed -i "/$HADOOP_OPTS/s/-XX:ParallelGCThreads=[0-9]*/-XX:ParallelGCThreads=$val/"  Hadoop-specific-environment.txt

above sed works fine but we have some problems

  1. in case key - ParallelGCThreads is null ( without value ) then replaced will failed

    ParallelGCThreads=

  2. value will replaced even line is in comment

    #export HADOOP_DATA_NODE_OPTS=

what are the right corrections in order to solved above issues

any other suggestion will welcome include with perl one liner


Solution

  • =[0-9]* should be replaced with (=[0-9]*)?.

    ^[^#]* should be added at the beginning of first regex.

    sed -i -E "/^[^#]*$HADOOP_OPTS/s/-XX:ParallelGCThreads(=[0-9]*)?/-XX:ParallelGCThreads=$val/" Hadoop-specific-environment.txt