Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何将一个文件中的行替换为另一个文件中的行_Linux_Shell_Unix - Fatal编程技术网

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

Linux 如何将一个文件中的行替换为另一个文件中的行,linux,shell,unix,Linux,Shell,Unix,我有一个像下面这样的文件,需要grep以system_props(^system_props)开头的行 我有另一个名为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

我有一个像下面这样的文件,需要grep以system_props(^system_props)开头的行

我有另一个名为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
现在我的要求是将
cat file1 | grep^ system_props
的内容替换为
cat file2 | grep^ system_props)

两个system_props值的预期输出应相同,并追加文件1中缺少的行编辑:,因为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
对于所显示的示例,输出将如下所示

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.

由于目前还不清楚,请您更清楚地发布输入和预期输出的样本。请编辑您的问题,然后让我们知道。您的意思是说
system\u props
对吗?请您确认一下,因为您的样品中只有这个关键词。嗨,Ravinder,是的,我的意思是“系统道具”。。这是打字错误
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.