Operating System - HP-UX
1753408 Members
7271 Online
108793 Solutions
New Discussion юеВ

Understanding Sed statement

 
Brecht De Baets
Frequent Advisor

Understanding Sed statement

Hi,

I have found the following statement to list only subdirectories which are not empty.

find . -type f | sed "s%/[^/]*$%%" | sort -u

I don't really understand the sed statement. I know it cuts the filenames away at the end of each line that was found by "find -type f", but I would like to be able to understand how
the statement actually is built.
Is there anyone who is able to explain the sed-part ?

Regards,
Brecht
2 REPLIES 2
Steven Schweda
Honored Contributor

Re: Understanding Sed statement

"s" is "substitute".

"%" is a delimiter. Normally, people use
"/" for that, but that's inconvenient when
looking for a "/" (and "sed" lets you use any
character as a delimiter).

So, it's looking for a "/", followed by any
number of non-"/" characters ("[^/]*") at the
end of a line ("$"), and substituting nothing
(what's between "%" and "%" in "%%") for
that.

man sed
Brecht De Baets
Frequent Advisor

Re: Understanding Sed statement

Ok thanks for the explanation.