简体   繁体   English

如何将一个文件中的行替换为另一个文件

[英]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_propscat 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.

相关问题 如何将行从一个文件替换/添加到另一个文件 - How to replace/add lines from one file to another file 如何用另一个文件中的内容替换一个文件中第XX行之后的所有行? - How do I replace all lines after line XX in one file with content from another file? 如何用另一个文件中的另一组替换一个文件中的一组字符串 - how to replace one group of strings in one file with another group from another file 从一个文件中提取与另一文件中的行相对应的数据 - Extract data from one file corresponding to lines from another file 从一个文件复制包含word的行到linux中的另一个文件 - copy lines containing word from one file to another file in linux 检查一个文件中的所有行是否都存在于另一个文件中 - Check if all lines from one file are present somewhere in another file 在Linux中将一个文件的值替换为另一文件的值 - Replace value from one file with value from another file in linux 如何将包含特定文本的行从一个文件复制到另一个文件 - How to Copy lines containing a specific text from one file to another file 如何从一个文件中获取所有在另一文件中包含字符串的行? - How to get all lines from one file which contain a string in another file? 将key:value从一个文件替换为shellscript的另一个文件? - Replace key:value from one file in another file in shellscript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM