简体   繁体   English

这个Sed命令究竟是如何工作的?

[英]How exactly does this Sed command work?

sed 's@.*/.*\.@.@'

The command is part of a larger command to find all file extensions in a directory. 该命令是查找目录中所有文件扩展名的较大命令的一部分。

find . -type f -name '*.*' | sed 's@.*/.*\.@.@' | sort | uniq

I understand that find returns all files with an extension, I understand that sed returns just the extensions and then sort/uniq are self-explanatory. 我理解find返回所有带扩展名的文件,我知道sed只返回扩展名,然后sort / uniq是不言自明的。

At first, I was confused about the @ symbol, but my best guess now guess is that it is part of Regex. 起初,我对@符号感到困惑,但我现在最好的猜测是它是Regex的一部分。

What really confuses me is a can't figure how it explicitly works, and the closest matching syntax I can find in a manual is s/regexp/new/ which still doesn't match the syntax of the command. 让我感到困惑的是无法确定它是如何显式工作的,而我在手册中找到的最接近的匹配语法是s/regexp/new/仍然与命令的语法不匹配。

In the s/regexp/replacement/ syntax, the / can be replaced by any other character, such as , , : , @ , etc. This is very useful if your regexp itself contains / characters, such as your example of .*/.*\\. s/regexp/replacement/语法中, /可以使用任何其他字符代替,比如,:@等,这是如果你的正则表达式本身就含有非常有用的/字符,比如你的例子.*/.*\\. .

Your command could be simplified a bit, though: 但是,您的命令可以简化一点:

find . -type f -name '*.*' | sed 's/.*\././' | sort -u

Here, I simplified the regexp so that it no longer contains a / character. 在这里,我简化了正则表达式,使其不再包含/字符。

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

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