I had a requirement where I needed to extract the third level sub domain name from a set of hostnames. "cut" alone was not enough to do the job as the hostnames where at different levels.
What I had was a set of hostnames like this ( This is not the real one that I worked on :P )
safeer@enjoyfast-lx:~/temp$ cat hosts.txt
www.safeer.in
dev.safeer.in
www.dev.safeer.in
mail.dev.safeer.in
mail.safeer.in
svn.dev.safeer.in
pop.mail.safeer.in
smtp.mail.safeer.in
blog.safeer.in
techlog.safeer.in
vm1.alpaha.dev.safeer.in
vm2.alpaha.dev.safeer.in
As you can see the hostnames are at different subdomain levels. So the only way is to extract the third field from right of the hostname. For this I made use of the "rev" command - which basically reverses a string and extracted the third filed as follows.
safeer@enjoyfast-lx:~/temp$ cat hosts.txt |rev|cut -d '.' -f3|rev
www
dev
dev
dev
mail
dev
mail
mail
blog
techlog
dev
dev
Did a sort to eliminate the duplicate entries. That did the trick and I got the result I needed
safeer@enjoyfast-lx:~/temp$ cat hosts.txt |rev|cut -d '.' -f3|rev|sort -u
blog
dev
mail
techlog
www
No comments:
Post a Comment