[SOLVED] finding names of directories - bash - sed
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yes it was really the sed or awk section I am looking for..
ideally it should give the full unknown-name1, while possibily dealing with spaces ((I imagine it needs to search for the second accurance of "/" and remove the namedir1 and any other "/"
There are lots of various web pages with sed one liners.
Search for a regexp that matches the pattern you describe, it simple for a greedy pattern matcher
try 's#.*/\(.*\)#\1#'
I guess I need another sed ((combo)) command or two to remove
/namedir1/
and all
"/"
So only the "unknown-name1" part is left...
Regards
Hyperdaz
No.
I thought you need first two parts..
Try this:
Code:
$ echo '/namedir1/unknown-name1/../../../../../file' | sed -rn 's|^/[^/]+/([^/]+).*$|\1|p'
unknown-name1
I will try to explain how it works. Your input string consists of a series of '/'-characters and "not '/'-characters" (that is almost any character except slash) between them. You need to extract second block of "not '/'-characters", right? Such a block (directory name) can be matched by regular expression '[^/]+', which reads "one or more consecutive not slashes". '\1' is a reference to text matched by expression in first parentheses. So we should skip first block and print only the second. The `s' (substitute) command uses the character after `s' to delimit its arguments: "s/.../.../" is the same as "s|...|...|"; this way we do not have to escape slashes in the regular expression.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.