How to replace/add lines from one file to another file

Sureshchandra Jarugula :

I have a one file like below example and I have requirement to grep the lines which are starting with system_props(cat file1 | grep ^system_props)..

JAVA_HOME=`find "$AGENT_HOME/jre" -name release -type f 2>/dev/null | sed "s|/release||g"`

system_props="$system_props -sensu.controller.hostName=abc.nam.net"
system_props="$system_props -sensu.controller.port=8181"
system_props="$system_props -sensu.controller.node=Mcagent"

if [ -z "$JAVA_HOME" ]; then
   if [ -d "/opt/middleware" ]; then
      JAVA_HOME=`find /opt/middleware -type d -name jre 2>/dev/null | grep WebSphere | grep java | grep -v grep | sort | uniq`
   fi
fi

I have another file called say file2 which having dummy content like below.

JAVA_HOME=`find "$AGENT_HOME/jre" -name release -type f 2>/dev/null | sed "s|/release||g"`

system_props="$system_props -sensu.controller.hostName=testhost.net"
system_props="$system_props -sensu.controller.port=8080"

if [ -z "$JAVA_HOME" ]; then
   if [ -d "/opt/middleware" ]; then
      JAVA_HOME=`find /opt/middleware -type d -name jre 2>/dev/null | grep WebSphere | grep java | grep -v grep | sort | uniq`
   fi
fi

Now my requirement is to replace the content of cat file1 | grep ^system_props to cat file2 | grep ^system_props)

The expected output of the system_props lines should be added in the file2 which are in file1 under same sequence.

system_props="$system_props -sensu.controller.hostName=abc.nam.net"
system_props="$system_props -sensu.controller.port=8181"
system_props="$system_props -sensu.controller.node=Mcagent"
RavinderSingh13 :

Could you please try following. Written and tested with samples.

awk '
FNR==NR{
  if(match($0,/system_props="/)){
    val=(val?val ORS:"")$0
  }
  next
}
/^system_props="/{
  if(++count==1){
    print val
  }
next
}
1
'  Input_file1   Input_file2

Explanation: Adding detailed explanation for above code.

awk '                                ##Starting awk program from here.
FNR==NR{                             ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
  if(match($0,/system_props="/)){    ##Checking condition if match for string system_props=" is found in current line then do following.
    val=(val?val ORS:"")$0           ##Creating variable val and keep appending current line value to its value here.
  }
  next                               ##next will skip all further statements from here.
}
/^system_props="/{                   ##Checking condition if line is starting from sting system_props=" then do following.
  if(++count==1){                    ##Checking condition if variable count is 1 then do following.
    print val                        ##Printing val variable here.
  }
  next                               ##next will skip all further statements from here.
}
1                                    ##1 will print edited/non-edited line here.
'  file1  file2                      ##Mentioning Input_file names here.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=6621&siteId=1