[英]How to replace lines from one file to another file
I have a one file like below and required to grep the lines which are starting with system_props(^system_props).我有一个像下面这样的文件,需要 grep 以 system_props(^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 as file2 which having dummy content like below.我有另一个名为 file2 的文件,其中包含如下所示的虚拟内容。
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
现在我的要求是替换cat file1 | grep ^system_props
的内容cat file1 | grep ^system_props
to cat file2 | grep ^system_props)
cat file1 | grep ^system_props
到cat file2 | grep ^system_props)
cat file2 | grep ^system_props)
the expected output of both system_props values should be same and append missing lines from file1两个 system_props 值的预期输出应该相同,并从 file1 中附加缺失的行
EDIT: Since OP has changed requirement a bit so adding edited solution here.编辑:由于 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
Could you please try following.你能不能试试以下。 This is going to substitute values from 1 file to another file by string system_props
occurrence., means 1st occurrence of string from File2 will be placed in first occurrence of string in File1.这将通过字符串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
For your shown samples output will be as follows.对于您显示的示例,输出如下。
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
Explanation: Adding detailed explanation for above code.说明:为上述代码添加详细说明。
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.