Score:1

How to grab only the 2nd-level domains from a list of subdomains

ma flag

What I need

I have a list of domains like so:

a.example.com
b.foo.com
a.b.bar.com

I only want the output to grab the second-level domains and nothing else, i.e., no 3rd-level or higher. This is what I'm looking for from my example list above:

example.com
foo.com
bar.com

What I tried

I've tried using sed, awk, and cut as follows:

sed

cat domains.txt | sed 's/\.$//g'
cat domains.txt | sed -r 's/^(.*)_/\1\\/; s/.$//g'  # this removes the last character for some reason

awk

awk '{ sub(/\.$/, ""); print $NF }' domains.txt
cat domains.txt | awk -F\. '{print $1,$2}' | tr ' ' '.' # won't work since there are 4th level domains

cut

cat domains.txt | cut -d '.' -f[field] # won't work since there are 4th level domains
bac0n avatar
cn flag
`rev domains.txt | cut -d . -f-2 | rev`
Justin Washek avatar
ma flag
That was annoyingly easy... thank you so much for the comment, this worked!
bac0n avatar
cn flag
you could use grep too: `grep -Po '[^.]+\.[^.]+$' domains`
bac0n avatar
cn flag
`while IFS=. read -a a; do echo ${a[@]:(-2):2}; done < domains`, if the list is short, probably the one I use.
Justin Washek avatar
ma flag
Awesome, thank you so much! I am always down to learn different ways to do things. You've been extremely helpful!
Will avatar
id flag
@bac0n - ?pop those as an answer - it’s obviously been helpful!
cn flag
The grep answer should also work with `-E`, nothing pcre-specific about that one.
bac0n avatar
cn flag
@glennjackman Not sure if it's just me, but `-E` is in some cases almost x3 slower than `-P` especially redirection.
Score:4
cn flag

In cases where you need to start your match from the right, you can use an end anchor $ to fixate the pattern to the end of the line.

grep:

grep -Po '[^.]+\.[^.]+$' domains.txt

sed:

sed  's/.*\.\([^.]\+\.[^.]\+\)$/\1/' domains.txt

awk has a pre-defined variable named NF holding the number of fields for the current record. You may combine the NF variable with the field specifier $ to reference the value instead.

awk:

awk -F . -vOFS=. '{print $(NF-1), $NF}' domains.txt

You can also reverse the text for commands like: read or cut that purely reads from left to right.

rev, cut:

rev domains.txt | cut -d . -f1,2 | rev

Bash only example:

while read -r; do \
    printf %s\\n ${REPLY/#${REPLY%.*.*}.}; \
done < domains.txt
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.