Solved

Bash sort / cut / uniq script to analyze domains

Posted on 2008-09-29
5
1,278 Views
Last Modified: 2013-12-26
I have a list of domains that I'd like to trim down a little bit. I only want the domains listed and not the subdomains of each domain. For example, the list below:

ff.search.yahoo.com
files.opensuse.org
files.widgetbox.com
yahoo.com
forums.dpreview.com
forums.opensuse.org
images.dpreview.com
geo.yahoo.com
go.microsoft.com
updates.vista.microsoft.com

I want to reduce to:

yahoo.com.com
opensuse.org
widgetbox.com
dpreview.com
microsoft.com

It would just be the cat's pajamas if I cut use the Cut command, specify the '.' as field separator and return the RIGHT two fields instead of numbering from the left. But I don't think that's possible, at least from what I've read and searched so far.

To say it another way, II only want to deal with two fields (as separated by '.'), not 4, 6, or three. And field quantity is a variable, depending on how many subdomains need to be deleted. I can't use AWK, but SEDis available to my systeme, if Ihave to use something other than Cut or Sort.
0
Comment
Question by:thinkwelldesigns
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
sed 's/.*\.\(.*\.\)/\1/' | sort -u
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
Why the restriction on awk?

awk -F. '{printf"%s.%s\n", $(NF-1),$NF}' file  |sort -u

otherwise I was going to suggest the same sed option as ozo.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
awk -F. '{d[$(NF-1),".",$NF]++}END{for( u in d) print u}'
0
 
LVL 9

Author Comment

by:thinkwelldesigns
Comment Utility
Thanks, ozo. The sed command works perfectly. I'm not quite sure exactly HOW it works, and if you'd give me an explanation, I'd be grateful. But it does work exactly so points to you!

Thanks a million.

BTW, this script will be used on a number of gateway machines that don't have awk installed, but do have sed. I dowanna hafta install awk on every machine to make the script work.

But is awk "better" than sed?
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
awk isn't better than sed, it's just another tool that is better at some tasks than sed.  In this instance, there's not much difference between using sed or awk.  I'm surprised you don't have awk on your gateway servers, as it is a very standard utility.

Anyway, breaking the sed statement down:

.*  zero or more matches of any character
\.  a literal dot
\(  start of pattern capture
.*\.  zero of more matches of any character terminated by a literal dot
\)  end of pattern capture
\1  print the captured pattern
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 48
strCopies  challenge 17 73
oracle query help 18 75
Adoquery sql  left join does not work 25 44
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now