LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2011, 09:09 AM   #1
hyperdaz
Member
 
Registered: Sep 2004
Location: UK
Distribution: CentOS 5.5
Posts: 44

Rep: Reputation: 1
finding names of directories - bash - sed


I have a number of directories that I know I want to run a bash script agenst.

I can find the file that I want with find / -iname "file"

/namedir1/unknown-name1/../../../../../file
/namedir2/unknown-name2/../../../../../file
/namedir3/unknown-name3/../../../../../file
/namedir4/unknown-name4/../../../../../file

I want to work out how to take

/namedir4/unknown-name*/ and remove the rest of the folder information while keeping the first part of the directory information.

I used to have a solution for such a issue but my mind has gone blank on what would be the best way to do the following.

Regards
Hyperdaz
 
Old 11-28-2011, 09:28 AM   #2
_bsd
Member
 
Registered: Jan 2010
Location: Velveeta, USA
Distribution: Xen, Gentoo,Ubuntu,openSUSE,Debian,pfSense
Posts: 98

Rep: Reputation: 9
Code:
dirname /namedir1/unknown-name1/../../../../../file
will return /namedir1/unknown-name1/../../../../../

then you can use a script or awk or perl or xargs or find -exec or whatever method works for you to use the returned path

conversely basename will return the filename from the string, optionally dropping suffix

Last edited by _bsd; 11-28-2011 at 09:29 AM.
 
Old 11-28-2011, 09:53 AM   #3
hyperdaz
Member
 
Registered: Sep 2004
Location: UK
Distribution: CentOS 5.5
Posts: 44

Original Poster
Rep: Reputation: 1
Thanks for you reply _BSD,

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 "/"

Cheers
Daz
 
Old 11-28-2011, 11:43 AM   #4
_bsd
Member
 
Registered: Jan 2010
Location: Velveeta, USA
Distribution: Xen, Gentoo,Ubuntu,openSUSE,Debian,pfSense
Posts: 98

Rep: Reputation: 9
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#'
 
Old 11-28-2011, 11:43 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Code:
$ echo '/namedir1/unknown-name1/../../../../../file' | sed -n 's|^\(/[^/]*/[^/]*/\).*$|\1|p'
/namedir1/unknown-name1/
$
or
Code:
$ echo '/namedir1/unknown-name1/../../../../../file' | sed -rn 's|^((/[^/]+){2}).*$|\1|p'
/namedir1/unknown-name1

Last edited by firstfire; 11-28-2011 at 11:50 AM.
 
Old 11-28-2011, 11:57 AM   #6
hyperdaz
Member
 
Registered: Sep 2004
Location: UK
Distribution: CentOS 5.5
Posts: 44

Original Poster
Rep: Reputation: 1
Thanks Firtfire

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
 
Old 11-28-2011, 12:16 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by hyperdaz View Post
Thanks Firtfire

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.

See `info sed', `info grep' for more details.

Hope this helps.

Last edited by firstfire; 11-28-2011 at 01:12 PM.
 
Old 11-28-2011, 01:17 PM   #8
_bsd
Member
 
Registered: Jan 2010
Location: Velveeta, USA
Distribution: Xen, Gentoo,Ubuntu,openSUSE,Debian,pfSense
Posts: 98

Rep: Reputation: 9
Did you not try the pattern that I gave you?
It works with sed
Code:
$echo '/namedir1/unknown-name1/../../../../../filename' | sed 's#.*/\(.*\)#\1#'
filename
 
Old 11-28-2011, 07:12 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@_bsd - OP is not looking for filename but rather 'unknown-name1' from your example.

How about we keep it simple:
Code:
echo '/namedir1/unknown-name1/../../../../../file' | awk -F/ '{print $3}'
 
1 members found this post helpful.
Old 11-29-2011, 04:35 AM   #10
hyperdaz
Member
 
Registered: Sep 2004
Location: UK
Distribution: CentOS 5.5
Posts: 44

Original Poster
Rep: Reputation: 1
_BSD

Yes I did try the example that you gave me, but I am not after the "filename" from the string but after "unknown-name1"

Sorry I thought that was clear from my post(s)

Many thanks grail, my thought was to use tr and then awk but -F is a little more faster

Thanks for all help

Hyperdaz
 
Old 11-29-2011, 05:12 AM   #11
_bsd
Member
 
Registered: Jan 2010
Location: Velveeta, USA
Distribution: Xen, Gentoo,Ubuntu,openSUSE,Debian,pfSense
Posts: 98

Rep: Reputation: 9
no, I'm sorry, I misread the initial post, didn't see what you were after, my bad.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
finding empty directories with bash Iriel Solaris / OpenSolaris 10 02-18-2016 03:59 AM
Bash: bash file names are different than openbox trash file names whatthefunk Programming 4 02-15-2011 01:52 AM
Little bash script and file to give long (or any) directories short names WardXmodem Programming 1 11-23-2009 12:24 AM
referencing directories with spaces in the names... bbneo Programming 7 06-23-2009 12:15 PM
Chaning names of directories and dependencies majam Linux - Software 1 01-29-2005 02:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration