Linux 仅输出第一个图案线及其下一行
我需要过滤命令的输出。 我试过这个Linux 仅输出第一个图案线及其下一行,linux,bash,awk,grep,Linux,Bash,Awk,Grep,我需要过滤命令的输出。 我试过这个 bpeek | grep nPDE 我的问题是我需要所有匹配的nPDE和找到的文件后面的行。因此,输出将如下所示: iteration nPDE 1 1 iteration nPDE 2 4 最好的情况是只显示一次找到的行,然后只显示后面的行 我找到了使用awk的解决方案,但据我所知,awk只能读取文件。有一个选项 grep --help ... -A, --after-context=NUM print NUM l
bpeek | grep nPDE
我的问题是我需要所有匹配的nPDE
和找到的文件后面的行。因此,输出将如下所示:
iteration nPDE
1 1
iteration nPDE
2 4
最好的情况是只显示一次找到的行,然后只显示后面的行
我找到了使用awk的解决方案,但据我所知,awk只能读取文件。有一个选项
grep --help
...
-A, --after-context=NUM print NUM lines of trailing context
因此:
bpeek | grep -A 1 'nPDE'
这是一种选择
grep --help
...
-A, --after-context=NUM print NUM lines of trailing context
因此:
bpeek | grep -A 1 'nPDE'
grep-A
如果您的grep支持它(它不在中),它就可以工作。如果没有,您可以使用sed:
bpeek | sed '/nPDE/!d;N'
以下哪项功能:
/nPDE/!d # If the line doesn't match "nPDE", delete it (starts new cycle)
N # Else, append next line and print them both
/nPDE/ { # If the line matches "nPDE"
$p # If we're on the last line, just print it
:a # Label to jump to
N # Append next line to pattern space
/\n[^\n]*nPDE[^\n]*$/! { # If appended line does not contain "nPDE"
p # Print pattern space
b # Branch to end (start new loop)
}
ba # Branch to label (appended line contained "nPDE")
}
请注意,这将无法打印此文件的正确输出
nPDE
nPDE
context line
如果您有GNU sed,您可以使用如下地址范围:
sed '/nPDE/,+1!d'
格式为addr1,+N
的地址定义了addr1
(在本例中为/nPDE/
)和以下N
行之间的范围。此解决方案更容易适应不同数量的上下文行,但在上面的示例中仍然失败
一种解决方案,用于管理以下情况:
blah
nPDE
context
blah
blah
nPDE
nPDE
context
nPDE
想要
sed -n '/nPDE/{$p;:a;N;/\n[^\n]*nPDE[^\n]*$/!{p;b};ba}'
执行以下操作:
/nPDE/!d # If the line doesn't match "nPDE", delete it (starts new cycle)
N # Else, append next line and print them both
/nPDE/ { # If the line matches "nPDE"
$p # If we're on the last line, just print it
:a # Label to jump to
N # Append next line to pattern space
/\n[^\n]*nPDE[^\n]*$/! { # If appended line does not contain "nPDE"
p # Print pattern space
b # Branch to end (start new loop)
}
ba # Branch to label (appended line contained "nPDE")
}
由于使用了-n
选项,因此不会打印所有其他行
正如Ed在评论中指出的那样,这既不可读,也不容易扩展到大量的上下文行,但对于一个上下文行是正确的。
grep-a
如果您的grep支持它(它不在上下文行中),它就可以正常工作。如果没有,您可以使用sed:
bpeek | sed '/nPDE/!d;N'
以下哪项功能:
/nPDE/!d # If the line doesn't match "nPDE", delete it (starts new cycle)
N # Else, append next line and print them both
/nPDE/ { # If the line matches "nPDE"
$p # If we're on the last line, just print it
:a # Label to jump to
N # Append next line to pattern space
/\n[^\n]*nPDE[^\n]*$/! { # If appended line does not contain "nPDE"
p # Print pattern space
b # Branch to end (start new loop)
}
ba # Branch to label (appended line contained "nPDE")
}
请注意,这将无法打印此文件的正确输出
nPDE
nPDE
context line
如果您有GNU sed,您可以使用如下地址范围:
sed '/nPDE/,+1!d'
格式为addr1,+N
的地址定义了addr1
(在本例中为/nPDE/
)和以下N
行之间的范围。此解决方案更容易适应不同数量的上下文行,但在上面的示例中仍然失败
一种解决方案,用于管理以下情况:
blah
nPDE
context
blah
blah
nPDE
nPDE
context
nPDE
想要
sed -n '/nPDE/{$p;:a;N;/\n[^\n]*nPDE[^\n]*$/!{p;b};ba}'
执行以下操作:
/nPDE/!d # If the line doesn't match "nPDE", delete it (starts new cycle)
N # Else, append next line and print them both
/nPDE/ { # If the line matches "nPDE"
$p # If we're on the last line, just print it
:a # Label to jump to
N # Append next line to pattern space
/\n[^\n]*nPDE[^\n]*$/! { # If appended line does not contain "nPDE"
p # Print pattern space
b # Branch to end (start new loop)
}
ba # Branch to label (appended line contained "nPDE")
}
由于使用了-n
选项,因此不会打印所有其他行
正如Ed在评论中指出的那样,这既不可读,也不容易扩展到大量的上下文行,但可以正确地用于一个上下文行。使用awk(为了完整性,因为您有grep和sed解决方案):
使用awk(为了完整性,因为您有grep和sed解决方案):
wrt-
据我所知,awk只能读取文件
。一点也不,它可以像grep
can一样从管道中读取,也可以从子进程和协同进程中读取。wrt-据我所知,awk只能读取文件。一点也不,它可以像grep
一样从管道中读取数据,也可以从子进程和协同进程中读取数据。这与grep或awk解决方案的语义不同-如果nPDE
可以出现在两个连续的行上,那么它将打印这两行,但不会打印第二行nPDE
之后的行。grep和awk解决方案将打印所有3行。只是说,……我觉得这不值得担心,因为这不是一个你无论如何都会使用sed的任务。想象一下,在nPDE
-键入sed'/nPDE/之后,要求更改为打印10行!DNNNNNN.…
会很快变老:-)而不是仅仅在grep或awk解决方案中更改数字。@EdMorton…那里!主要是为了证明您应该使用awk;)这与grep或awk解决方案的语义不同-如果nPDE
可以出现在两个连续的行上,那么它将打印这两行,但不会打印第二行nPDE
之后的行。grep和awk解决方案将打印所有3行。只是说,……我觉得这不值得担心,因为这不是一个你无论如何都会使用sed的任务。想象一下,在nPDE
-键入sed'/nPDE/之后,要求更改为打印10行!DNNNNNN.…
会很快变老:-)而不是仅仅在grep或awk解决方案中更改数字。@EdMorton…那里!主要是为了证明您应该使用awk;)