I am trying to dynamically find a directory the be used programmatically later on in a script. The problem that I am having is accounting for white space which may or may not be there.
Using the following example because it outputs three strings separated by spaces (paths in this case). Let's assume I want to get the directory for the man pages for a particular command (forgetting, for a moment, that there are builtin ways to do this) using whereis
:
$ whereis bash
bash: /bin/bash /usr/local/man/man1/bash.1.gz /usr/ports/shells/bash
I would like to extract any one of the directories. Using sed
, I came up with the following:
$ whereis bash | sed -En 's:.*[" "](.*man.*)[" "].*:\1:p'
/usr/local/man/man1/bash.1.gz
That works great if pattern happens to be in the middle, but if it happens to be at the beginning or ending of the string, I have to remove the space from the pattern to get it to work (using "port" for the pattern as an example)
$ whereis bash | sed -En 's:.*[" "](.*port.*)[" "].*:\1:p'
$ whereis bash | sed -En 's:.*[" "](.*port.*).*:\1:p'
/usr/ports/shells/bash
The same holds true if I wanted to extract the directory with the pattern "bin" in it.
How do I "tell" sed
that the pattern may contain a certain character.
Why am I doing this?
When I try it without the spaces, I get the following:
$ whereis bash | sed -En 's:.*(.*man.*).*:\1:p'
man1/bash.1.gz /usr/ports/shells/bash
I don't get the full path of the text I wanted and it adds on a path that I totally don't want. The space is a delimiter.
I have used this post: How to output only captured groups with sed? and this post: sed - how to do regex groups using sed as reference and a jumping off point.
Also of note, I tried using the regex \s
for white space, but it was ignored. I'm also on FreeBSD so I am using the -E
for regex.
If there's another way to approach this, a point in the right direction would be greatly appreciated; I;m very very new to working with sed
and awk
.