我正在尝试使用awk来读取文件,并且只显示不以+或 - 4行或更多次开头的行。 gawk也没关系。每个分组用空行分隔。
以下是文件中的示例,这些是我不想要打印的行:
+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
我做要打印的文件中的样本(不是连续4个或更多):
-Not shown: 76 closed ports, 18 filtered ports
+Not shown: 93 closed ports
PORT STATE SERVICE VERSION
+514/tcp open shell
我正在学习如何使用awk,因为我一直在阅读O'Reilly的awk&但是我对这个问题感到有些困惑。此外,如果有人关心,我不介意看到使用shell脚本解决此问题的非awk方法。
谢谢!
答案 0 :(得分:5)
如果我理解了您的问题,输入文件会将记录作为段落,因此您需要将它们用空行分隔。我认为它适用于下一个脚本:
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
答案 1 :(得分:-1)
awk '{if(NF>3 &&( $0 ~ /\+/ || $0 ~ /-/) ) print $0}' test.txt