简体   繁体   中英

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.

At first, I was confused about the @ symbol, but my best guess now guess is that it is part of 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.

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 .*/.*\\. .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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