如果我理解你的问题,输入文件的记录是段落,所以你需要用空行分隔它们。我假设它用于下一个脚本:
内容script.awk
:
BEGIN {
## Separate records by one or more blank lines.
RS = ""
## Each line will be one field. Both for input and output.
FS = OFS = "\n"
}
## For every paragraph...
{
## Flag to check if I will print the paragraph to output.
## If 1, print.
## If 0, don't print.
output = 1
## Count how many consecutive rows have '+' or '-' as first
## character.
j = 0
## Traverse all rows.
for ( i = 1; i <= NF; i++ ) {
if ( substr( $i, 1, 1 ) ~ /+|-/ ) {
++j;
}
else {
j = 0
}
if ( j >= 4 ) {
output = 0
break
}
}
if ( output == 1 ) {
print $0 "\n"
}
}
假设以下测试输入文件为infile
:
+Host is up.
+Not shown: 95 closed ports, 3 filtered ports
+PORT STATE SERVICE VERSION
+Host is up.
+Not shown: 95 closed ports, 3 filtered ports
+PORT STATE SERVICE VERSION
+23/tcp open telnet
+9100/tcp open jetdirect
-Host is up.
-Not shown: 99 closed ports
-PORT STATE SERVICE VERSION
-5900/tcp open vnc
-Not shown: 76 closed ports, 18 filtered ports
+Not shown: 93 closed ports
PORT STATE SERVICE VERSION
+514/tcp open shell
像这样运行脚本:
awk -f script.awk infile
使用以下输出(第一个记录,因为它没有达到四个连续的行,第二个记录,因为它们之间有不同的行):
+Host is up.
+Not shown: 95 closed ports, 3 filtered ports
+PORT STATE SERVICE VERSION
-Not shown: 76 closed ports, 18 filtered ports
+Not shown: 93 closed ports
PORT STATE SERVICE VERSION
+514/tcp open shell