我有一个像下面例子一样的一个文件,我需要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行的预期输出应添加到文件2中,这些行在文件1中的顺序相同。
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"
请您尝试以下操作。用样品书写和测试。
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大致相同的答案。
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的方法,因为它更短(尽管读者可能不太清楚(。
您想要连接三个文件段:
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