简体   繁体   English

这个sed命令的含义是什么? sed's%^。* / %%'

[英]What's the meaning of this sed command? sed 's%^.*/%%'

I saw a bash command sed 's%^.*/%%' 我看到一个bash命令sed 's%^.*/%%'

Usually the common syntax for sed is sed 's/pattern/str/g' , but in this one it used s%^.* for the s part in the 's/pattern/str/g' . 通常sed的常用语法是sed 's/pattern/str/g' ,但是在这个中它使用s%^.*作为's/pattern/str/g's部分。

My questions: 我的问题:
What does s%^.* mean? s%^.*是什么意思?
What's meaning of %% in the second part of sed 's%^.*/%%' ? sed 's%^.*/%%'的第二部分中%%的含义是什么?

The % is an alternative delimiter so that you don't need to escape the forward slash contained in the matching portion. %是一个替代分隔符,因此您无需转义匹配部分中包含的正斜杠。

So if you were to write the same expression with / as a delimiter, it would look like: 因此,如果您使用/作为分隔符编写相同的表达式,它将如下所示:

sed 's/^.*\///'

which is also kind of difficult to read. 这也有点难以阅读。

Either way, the expression will look for a forward slash in a line; 无论哪种方式,表达式都会在一行中寻找正斜杠; if there is a forward slash, remove everything up to and including that slash. 如果有正斜杠,则删除所有包含斜杠的内容。

the usually used delimiter is / and the usage is sed 's/pattern/str/' . 通常使用的分隔符是/ ,用法是sed 's/pattern/str/'

At times you find that the delimiter is present in the pattern. 有时您会发现模式中存在分隔符。 In such a case you have a choice to either escape the delimiter found in the pattern or to use a different delimiter. 在这种情况下,您可以选择转义模式中找到的分隔符或使用不同的分隔符。 In your case a different delimiter % has been used. 在您的情况下,使用了不同的分隔符%

The later way is recommended as it keeps your command short and clean . 建议使用后面的方法,因为它可以使您的命令保持简洁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM