【问题标题】:How to replace/add lines from one file to another file如何将一个文件中的行替换/添加到另一个文件
【发布时间】:2020-02-17 12:36:16
【问题描述】:

我有一个类似下面示例的文件,我需要 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

我有另一个名为 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

现在我的要求是将cat file1 | grep ^system_props的内容替换为cat file2 | grep ^system_props)

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"

【问题讨论】:

  • 花一些时间寻找好的例子。您不希望 cp file1 file2 作为答案。
  • 注意:cat file1 | grep ^system_props 中的cat file1 是一个UUOc(不必要地使用cat) .命令是grep '^system_props' file1。除非您实际上是在连接两个文件,否则如果您发现自己在做 cat ....,那么它很可能是一个 UUOc...

标签: linux shell unix awk


【解决方案1】:

请您尝试以下操作。使用示例编写和测试。

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

说明:为上述代码添加详细说明。

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.

【讨论】:

  • 谢谢你,Ravinder,这个解决方案按预期对我有用。
【解决方案2】:

我没有查看现有答案就试了一下,得出的答案与 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

就个人而言,我更喜欢 Ravinder 的解决方案。他的count 变量与我的nocopy 变量相同。他使用单个变量从第一个文件中捕获 system_props,而我使用数组。

此外,Ravinder 正在 awk 中寻找“^system_props”行,而我将这个责任委托给 grep。但是,有些人可能会觉得我用于 grep 的

我还注意到,Ravinder 使用了 1 的 awk 习语来表示始终打印,这与我用 {print} 拼写的内容相同。我喜欢 Ravinder 的方法,因为它更短(尽管对读者来说可能不太明显)。

【讨论】:

    【解决方案3】:

    您想连接三个文件段:
    1.第二个文件直到第一个^system_props
    2. 第一个文件中带有^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
    

    一起:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多