简体   繁体   English

如何将行从一个文件替换/添加到另一个文件

[英]How to replace/add lines from one file to another file

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 )..我有一个如下例所示的文件,我需要 grep 以 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.我有另一个名为 say 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 the system_props lines should be added in the file2 which are in file1 under same sequence. system_props 行的预期输出应以相同的顺序添加到 file2 中,它们位于 file1 中。

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"

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.

I gave this a go without looking at the existing answers and came up with roughly the same answer as Ravinder.我在没有查看现有答案的情况下试了一下,并得出了与 Ravinder 大致相同的答案。

awk '
FNR == NR  { 
  line[FNR] = $0 
  next 
}  
/^system_props/  { 
  if (!nocopy) 
    for (x = 0 ; x < length(line) ; x++ ) 
      print line[x]  
  nocopy=1 
  next 
} 
{ 
  print 
} '  <( grep ^system_props file1 )  file2

Personally, I like Ravinder's solution better.就个人而言,我更喜欢 Ravinder 的解决方案。 His count variable is the same as my nocopy variable.他的count变量与我的nocopy变量相同。 And he uses a single variable to capture the system_props from the first file whereas I use an array.他使用单个变量从第一个文件中捕获 system_props,而我使用数组。

Also, Ravinder is looking for "^system_props" lines within awk, whereas I relegate that responsibility to grep.此外,Ravinder 正在 awk 中寻找“^system_props”行,而我将该责任委托给 grep。 However, some may feel that the <( ) syntax I'm using for the grep may be unnecessarily complex.但是,有些人可能会觉得我用于 grep 的 <( ) 语法可能过于复杂。

I also noticed that Ravinder used an awk idiom of 1 to indicate always print which is equivalent to the what I spelled out with {print} .我还注意到 Ravinder 使用 awk 惯用语1来表示始终打印,这相当于我用{print}拼出的内容。 I like Ravinder's approach because it's shorter (albeit perhaps less obvious to the reader).我喜欢 Ravinder 的方法,因为它更短(尽管对读者来说可能不太明显)。

You want to concatenate three file segments:您想连接三个文件段:
1. The second file until the first ^system_props 1.第二个文件直到第一个^system_props
2. The lines from the first file with ^system_props 2. 带有^system_props的第一个文件中的行
3. The second file after the last ^system_props 3.最后一个^system_props之后的第二个文件

# Part 1
sed '/^system/,$ d' file2
# Part 2
grep '^system' file1
# Or sed -n '/^system/p' file1
# Part 3
sed '1,/^system/d; /^system/ d' file2

Together:一起:

sed '/^system/,$ d' file2; grep '^system' file1; sed '1,/^system/d; /^system/ d' file2

This approach can be done with awk too:这种方法也可以用awk来完成:

awk -F= 'BEGIN {show=1}
         ARGIND==1 && $1=="system_props" {show=0}
         ARGIND==2 {show=($1=="system_props" ? 1:0)}
         ARGIND==3 && $1=="system_props" {show=1; next}
         show {print}' file2 file1 file2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM