【发布时间】: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...