[techtalk] need help with a shell script
Andrew Wendt
awendt at putergeek.com
Tue Sep 26 17:50:21 EST 2000
On Tue, 26 Sep 2000, Laurel Fan wrote:
>Here's a way to get the [domain-minus-top-level-domain] part:
>
> echo $DOMAIN | sed -e 's/\.[^\.]*$//'
If you're using bash, you can also do this directly from the shell:
echo "${DOMAIN%.*}"
This will take the shortest amount of text matching the glob pattern .* from
the end of your variable and throw it away.
You can also use %% instead of % to throw away the longest amount of text
matching the pattern. Similarly, # and ## throw away the shortest and longest
match from the beginning of your variable.
So:
---snip---
$ DOMAIN=foo.bar.com
$ echo "${DOMAIN%.*}"
foo.bar
$ echo "${DOMAIN%%.*}"
foo
$ echo "${DOMAIN#*.}"
bar.com
$ echo "${DOMAIN##*.}"
com
---snip---
>now for the [first-letter-of-domain] part:
>
> echo $DOMAIN | cut -c 1
You can also do this from the shell.
---snip---
$ echo "${DOMAIN:0:1}"
f
---snip---
The first number is which letter to start at (counted from zero), and the
next is the length you want.
By the way, in your usage of sed here:
echo $DOMAIN | sed -e 's/\.[^\.]*$//'
You needn't escape the dot inside your square brackets. And in fact by trying
to I think you have made a pattern which matches a dot followed by some text
which isn't another dot or a backslash. So it wouldn't properly work with a
name like "foo.bar.c\m".
Which shouldn't matter since you don't see many backslashes in domain names.
:-)
TTFN
Andy
More information about the Techtalk
mailing list