gpt4 book ai didi

awk - Grep 多个模式并打印模式及其前几行

转载 作者:行者123 更新时间:2023-12-04 03:39:07 24 4
gpt4 key购买 nike

我有一个包含数据的文件

cell A
function (A1A2)
Next state
Test cell
Type combinational
(CO)
output
(A1)
input
(A2)
input
cell X
function ((A1+A2)B)
Hold rising
Type combinational
Min_pulse_width
(Z)
output
(A1)
input
(A2)
input
(B)
input

我想要输出为

cell A
function (A1A2)
Type combinational
(CO)
output
(A1)
input
(A2)
input
cell X
function ((A1+A2)B)
Type combinational
(Z)
output
(A1)
input
(A2)
input
(B)
input

输入文件包含多行以上显示的格式,其中包含许多参数。但是从那个文件我只想grep cell, function, type, output and its previous line , input and its previous line

我可以使用 grep cell, function, type

grep "cell|function|type" file

并且可以 grep output and its previous line and input And it's previous line as

awk '/input/{print x;print};{x=$0}' file

awk '/output/{print x;print};{x=$0}' file

但是我怎样才能同时执行以上 3 个步骤并获得显示的输出。

最佳答案

你可以试试这个awk:

awk '/^(in|out)put/{print p; print} /^(cell|function|Type)/; {p = $0}' file

cell A
function (A1A2)
Type combinational
(CO)
output
(A1)
input
(A2)
input
cell X
function ((A1+A2)B)
Type combinational
(Z)
output
(A1)
input
(A2)
input
(B)
input

关于awk - Grep 多个模式并打印模式及其前几行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66366880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com