Regex 替换与包含斜杠的字符串匹配的行?

Regex 替换与包含斜杠的字符串匹配的行?,regex,sed,Regex,Sed,我有这样一个文件: /foo/bar/asdf asdf/foo/bar/qwerty /qwerty/foo/asdf /asdf/qwerty/bar 我想删除任何不包含/bar/的行,以便所需的输出为: /foo/bar/asdf asdf/foo/bar/qwerty 我尝试使用不同的字符作为分隔符,例如冒号:sed\:/blocks/:!d文件-i,但它似乎没有删除任何内容。尝试此sed命令 sed -n '/\/bar\//p' file 它打印包含/bar/字符串的所有行 示

我有这样一个文件:

/foo/bar/asdf
asdf/foo/bar/qwerty
/qwerty/foo/asdf
/asdf/qwerty/bar
我想删除任何不包含
/bar/
的行,以便所需的输出为:

/foo/bar/asdf
asdf/foo/bar/qwerty
我尝试使用不同的字符作为分隔符,例如冒号:
sed\:/blocks/:!d
文件
-i
,但它似乎没有删除任何内容。

尝试此sed命令

sed -n '/\/bar\//p' file
它打印包含
/bar/
字符串的所有行

示例:

$ cat rr.txt
/foo/bar/asdf
asdf/foo/bar/qwerty
/qwerty/foo/asdf
/asdf/qwerty/bar

$ sed -n '/\/bar\//p' rr.txt
/foo/bar/asdf
asdf/foo/bar/qwerty
试试这个sed命令

sed -n '/\/bar\//p' file
它打印包含
/bar/
字符串的所有行

示例:

$ cat rr.txt
/foo/bar/asdf
asdf/foo/bar/qwerty
/qwerty/foo/asdf
/asdf/qwerty/bar

$ sed -n '/\/bar\//p' rr.txt
/foo/bar/asdf
asdf/foo/bar/qwerty

一种
awk
解决方案

awk '/\/bar\//' file
/foo/bar/asdf
asdf/foo/bar/qwerty

一种
awk
解决方案

awk '/\/bar\//' file
/foo/bar/asdf
asdf/foo/bar/qwerty

放置sed inline edit
-i
选项可对文件进行更改。放置sed inline edit
-i
选项可对文件进行更改。您只需将
sed
表达式置于引号中,即
sed'\:/bar/:!d'file
@devnull谢谢,这也行。您只需将
sed
表达式用引号括起来,即
sed'\:/bar/:!d'file
@devnull谢谢,这也行。