РЕДАКТИРОВАТЬ: Поскольку OP немного изменил требование, добавьте отредактированное решение здесь.
awk '
FNR==NR{
if(match($0,/^system_props=".*/)){
a[++count]=substr($0,RSTART+14,RLENGTH-14)
}
next
}
match($0,/^system_props="/){
$0=substr($0,RSTART,RLENGTH) a[++count1]
}
1;
END{
if(count!=count1){
while(++count1<=count){
print a[count1]
}
}
}
' File2 File1
Не могли бы вы попробовать следующее , Это заменит значения из 1 файла в другой файл строкой system_props
вхождения. Это означает, что 1-е вхождение строки из File2 будет помещено в первое вхождение строки в File1.
awk '
FNR==NR{
if(match($0,/^system_props=".*/)){
a[++count]=substr($0,RSTART+14,RLENGTH-14)
}
next
}
match($0,/^system_props="/){
$0=substr($0,RSTART,RLENGTH) a[++count1]
}
1
' Input_file2 Input_file1
Для показанных вами примеров выходные данные будут следующими:
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
Объяснение: Добавление подробного объяснения для приведенного выше кода.
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when file2 is being read.
if(match($0,/^system_props=".*/)){ ##Checking condition if line has system_props=" then do following.
a[++count]=substr($0,RSTART+14,RLENGTH-14) ##Creating array a with index variable count(whose value is increasing with 1) and its value is substring of current line with starting point of RSTART and ending point of RLENGTH.
}
next ##next will skip all further lines from here.
}
match($0,/^system_props="/){ ##Checking condition if a line starts from
$0=substr($0,RSTART,RLENGTH) a[++count1] ##Assigning substring of current line from RSTART to RLENGTH and putting value of array a which we collected from previous file.
}
1 ##1 will print edited/non-edited lines of Input_file1 here.
' File2 File1 ##Mentioning Input_file names here.