[英]search through regex pattern in a file
I have a file with data in below format我有一个包含以下格式数据的文件
abc {u_bit_top/connect_down/u_FDIO[6]/u_latch}
ghi {u_bit_top/seq_connect/p_REDEIO[9]/ff_latch
def {u_bit_top/connect_up/shift_reg[7]
I want to search for pattern *bit_top*FDIO*
and *bit_top*REDEIO*
in the file in each line and delete the complete line if pattern is found.我想在每一行的文件中搜索模式
*bit_top*FDIO*
和*bit_top*REDEIO*
,如果找到模式则删除整行。 I want output as我想要 output 作为
def {u_bit_top/connect_up/shift_reg[7]
I did using sed like sed "/bit_top/d;/FDIO/d;/REDEIO/d;"
我确实使用了 sed 就像
sed "/bit_top/d;/FDIO/d;/REDEIO/d;"
but this deletes the line having bit_top and FDIO and REDEIO separately.但这会删除分别具有bit_top和FDIO和REDEIO的行。 How I can search for above pattern and delete the line containing it.
我如何搜索上述模式并删除包含它的行。 Shell or TCL anything will be useful.
Shell 或 TCL 任何东西都会有用。
Since you tagged tcl因为你标记了 tcl
set fh [open "filename"]
set contents [split [read -nonewline $fh] \n]
close $fh
set filtered [lsearch -inline -not -regexp $contents {bit_top.*(FDIO|REDEIO)}]
results in结果是
def {u_bit_top/connect_up/shift_reg[7]
lsearch
documentation .lsearch
文档。
But really all you need for this is grep但实际上你只需要grep
grep -Ev 'bit_top.*(FDIO|REDEIO)' filename
You've been close;你一直很亲密; ;)
;)
sed '/bit_top.*FDIO/d' input
Just input a regex to sed that matches what you want...只需将正则表达式输入 sed 即可匹配您想要的...
Using sed
使用
sed
$ sed -E '/bit_top.*(REDE|FD)IO/d' input_file
def {u_bit_top/connect_up/shift_reg[7]
You might use GNU AWK
for this task following way, let file.txt
content be您可以按照以下方式使用 GNU
AWK
完成此任务,让file.txt
内容为
abc {u_bit_top/connect_down/u_FDIO[6]/u_latch}
ghi {u_bit_top/seq_connect/p_REDEIO[9]/ff_latch
def {u_bit_top/connect_up/shift_reg[7]
then然后
awk '/bit_top/&&(/FDIO/||/REDEIO/){next}{print}' file.txt
gives output给出 output
def {u_bit_top/connect_up/shift_reg[7]
Explanation: if lines contain bit_top
AND ( FDIO
OR REDEIO
) then go to next
line ie skip it.解释:如果行包含
bit_top
AND ( FDIO
OR REDEIO
) 那么 go 到next
行,即跳过它。 If that did not happen line is just print
ed.如果那没有发生,那一行就被
print
。
(tested in GNU Awk 5.0.1) (在 GNU Awk 5.0.1 中测试)
With a small change you can implement the compound pattern (eg, *bit_top*FDIO*
) in sed
.稍作改动,您就可以在 sed 中实现复合模式(例如
*bit_top*FDIO*
sed
。
A couple variations on OP's current sed
: OP 当前
sed
的几个变体:
# daisy-chain the 2 requirements:
$ sed "/bit_top.*FDIO/d;/bit_top.*REDEIO/d" file
def {u_bit_top/connect_up/shift_reg[7]
# enable "-E"xtended regex support:
$ sed -E "/bit_top.*(FDIO|REDEIO)/d" file
def {u_bit_top/connect_up/shift_reg[7]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.