我有以下输入文件:
This is just junk
stuff this should not
appear in the output
module instance1 (
.a (x),
.b (y),
.c (z)
);
Module instance2 (
.a (x),
.b (y),
.c (z)
);
So is this part of the
file so no output here
as well`<br>
I want the following output:<br>
`module instance1 (
.a (x),
.b (y),
.c (z)
);
Module instance2 (
.a (x),
.b (y),
.c (z)
);
我尝试运行以下脚本:
#!/bin/bash
block=`sed -n '/($/p' test1
echo $block
for bloc in "$block"; do
sed -n "/$bloc/,/);/{p}" test1
done
但是我收到以下错误:
sed: -e expression #1, char 19: unterminated address regex
但是为什么上述脚本没有工作呢?
修改:test1是上面显示的输入文件
答案 0 :(得分:2)
当shell在sed脚本中展开$bloc
时,它最终会显示:
sed -n "/module instance1 (
Module instance2 (
Module instance2 (/,/);/{p}" test1
正如您可能想象的那样,它并不高兴。
如果您希望sed
仅打印出module .... (
和);
之间的行,那么只需直接执行此操作而不是玩这个游戏
sed -n '/^[mM]odule .* ($/,/);/p'
如果你需要它们在单独的文件或其他东西,那么你需要修复你如何找到模块名称和循环起始行
答案 1 :(得分:-1)
sed -n "/${bloc:-^}/,/);/ {p;}" test1
;
(或根据需要添加新行)^
。这避免了//
模式测试
bloc='Module instance2 ('
sed -n "/${bloc:-^}/,/);/ {p;}" YourFile
Module instance2 (
.a (x),
.b (y),
.c (z)
);
Module instance2 (
.a (x),
.b (y),
.c (z)
);